Fundamentals 11 min read

Common Python Syntax and Runtime Errors with Practical Fixes

This guide enumerates frequent Python errors—including syntax, type, value, attribute, file, indentation, index, key, module, name, recursion, and timeout issues—explains why they occur, and provides concise, actionable solutions to help developers quickly diagnose and correct their code.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Common Python Syntax and Runtime Errors with Practical Fixes

SyntaxError: invalid syntax – Often caused by missing or incorrect colons after statements like if , while , or for , unmatched parentheses, or using an assignment operator instead of a comparison. Fix by adding the proper colon, closing brackets, or using == for comparisons.

Syntax Error: non-default argument follows default argument – Occurs when a function defines a parameter with a default value before a parameter without one. Reorder the parameters so that all non‑default arguments precede default arguments.

SyntaxError: invalid character in identifier – Triggered by illegal characters (e.g., @, #, spaces) or identifiers starting with a digit. Use only letters, digits, and underscores, and start with a letter or underscore.

TypeError: can only concatenate str (not “XXX”) to str – Happens when attempting to concatenate a string with a non‑string object. Convert the non‑string value using str() before concatenation.

TypeError: f() takes exactly 2 arguments (1 given) – Raised when a function is called with fewer arguments than required. Supply the missing arguments or adjust the function definition.

TypeError: ‘tuple’ object does not support item assignment – Tuples are immutable; trying to modify an element causes this error. Convert the tuple to a list, modify, then convert back if needed.

ValueError: could not convert string to float: ‘12.2s’ – Occurs when float() receives a non‑numeric string. Ensure the string contains only numeric characters or remove units before conversion.

ValueError: invalid literal for int() with base 10 – Raised when int() receives a string with non‑digit characters or a decimal point. Use float() first if the value may be a float, then cast to int , or validate the string with str.isdigit() .

ValueError: substring not found – Happens when methods like index() or find() cannot locate a substring. Verify the substring exists before searching.

ZeroDivisionError: division by zero – Triggered by dividing by zero. Check the divisor before performing division and handle the case with conditional logic.

AttributeError: ‘module’ object has no attribute … – Caused by misspelling an attribute, using a Python keyword as a filename, or accessing a module via the wrong alias. Correct the spelling, rename the file, or use the proper alias.

AttributeError: ‘Obj’ object has no attribute ‘attr’ – Results from misspelling the attribute name, trying to access a private attribute, or using an object method on the wrong type. Verify the attribute name, respect naming conventions, and call methods on appropriate objects.

FileExistsError: [Errno 17] File exists – Occurs when attempting to create a file that already exists. Check for the file’s existence before creation.

FileNotFoundError: [Errno 2] No such file or directory – Raised when a specified file or directory cannot be found. Verify the path is correct.

IndentationError: expected an indented block – Missing indentation after statements like def , if , for , etc. Add the required indentation.

IndentationError: unexpected indent – Extra or inconsistent indentation. Remove the superfluous spaces or tabs.

IndentationError: unindent does not match any outer indentation level – Mixed indentation styles within the same block. Use a consistent indentation scheme (e.g., 4 spaces).

IndexError: list index out of range – Accessing a list element beyond its length or when the list is empty. Ensure the index is within bounds and the list is non‑empty.

KeyError: ‘age’ – Attempting to retrieve a dictionary key that does not exist. Use an existing key or check with in before access.

ModuleNotFoundError: No module named ‘pymysql’ – The module is not installed, misspelled, or not available in the current environment. Install the module, correct the name, or activate the appropriate virtual environment.

NameError: name ‘test’ is not defined – Refers to an undefined variable or a variable out of scope. Define the variable before use or adjust its scope.

RecursionError: maximum recursion depth exceeded – Caused by infinite or excessively deep recursion. Limit recursion depth or rewrite the algorithm iteratively.

Timeout error [WinError 10060] – Network or database connection timed out. Verify connection parameters, URLs, and server availability.

DebuggingProgrammingsyntax errorruntime error
Python Programming Learning Circle
Written by

Python Programming Learning Circle

A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.