Troubleshooting referenceUpdated October 2026•7 min read

Common Python errors and troubleshooting guide

Python tracebacks report the exact file name, line number, and exception type when a script crashes. Understanding how to read tracebacks allows you to identify code errors and resolve execution failures quickly.

Need to debug an error?
Paste your code into the runner to see educational explanations and click-to-line error jumping.
Open Python runner

Anatomy of a Python traceback

When the CPython 3.12 interpreter encounters an unhandled exception, it formats an execution traceback across standard error. The traceback prints chronologically from the initial call frame down to the specific line that triggered the crash.

# Standard traceback format
Traceback (most recent call last):
File "/main.py", line 14, in compute_average
return total / count
ZeroDivisionError: division by zero

The final line names the exception class (ZeroDivisionError) and provides descriptive text explaining the failure condition. The preceding lines indicate that the failure happened inside compute_average on line 14 of main.py.

Ten frequent Python errors and their solutions

These ten exception types account for the majority of execution bugs in Python scripts.

1. SyntaxError: invalid syntax

The interpreter raises a SyntaxError during the parsing stage before any code executes. This occurs when you omit a colon after an if statement or leave a parenthesis open.

# Incorrect: if x == 10 print(x)
# Correct: if x == 10: print(x)

2. IndentationError: unexpected indent

Python uses whitespace indentation to delimit code blocks instead of curly braces. Mixing tabs and spaces triggers an IndentationError. Set your text editor to insert 4 spaces when pressing the Tab key.

# Incorrect mixing of indentation depths
def greet():
print("Hello") # 2 spaces
print("World") # 4 spaces triggers IndentationError

3. NameError: name 'variable' is not defined

A NameError indicates that your code referenced an identifier that does not exist in local or global scope. Common causes include misspelled variable names or referencing a variable before its assignment statement.

# NameError: total_score is misspelled as totle_score
total_score = 95
print(totle_score)

4. TypeError: can only concatenate str (not "int") to str

Python does not perform implicit string coercion when using the addition operator. You must explicitly convert numbers with str() or use Python 3.6 f-strings.

# Incorrect: print("Score: " + 95)
# Correct: print(f"Score: {95}")

5. ValueError: invalid literal for int() with base 10

This exception occurs when passing arbitrary strings into numerical conversion functions such as int() or float(). Validate input with str.isdigit() or enclose the conversion inside a try-except block.

try:
age = int(input("Enter age: "))
except ValueError:
age = 0

6. IndexError: list index out of range

Python lists use zero-based indexing. A list with 5 elements contains indices from 0 through 4. Requesting index 5 raises an IndexError.

# IndexError
items = [10, 20, 30]
print(items[3]) # valid indices are 0, 1, 2

7. KeyError: 'key_name'

Directly indexing a dictionary with square brackets raises a KeyError if the key is missing. Use the dict.get() method with a fallback default value to avoid exceptions.

config = {"theme": "light"}
# Safe lookup returning None if key is missing:
font_size = config.get("fontSize", 14)

8. ZeroDivisionError: division by zero

Dividing a number by zero or performing a modulo operation by zero triggers this exception. Always test divisors for non-zero values before arithmetic operations.

9. ModuleNotFoundError: No module named 'requests'

This exception occurs when your script imports a third-party library that is not present in sys.path. In Python Code Runner, open the Packages tab and install the library via micropip. On Windows, execute pip install requests in PowerShell.

10. AttributeError: 'NoneType' object has no attribute 'append'

This error happens when you assign the return value of an in-place list method to a variable. In Python, methods like list.sort() and list.append() mutate the list in place and return None.

# Incorrect: numbers = numbers.append(4) -> numbers becomes None
# Correct: numbers.append(4)

Frequently asked questions about online Python execution

Why does input() raise an EOFError in some online runners?

Many online compilers run code in non-interactive batch jobs where standard input closes immediately. Python Code Runner avoids this by keeping an in-memory input buffer or prompting the user dynamically when input() is called.

Can I access the local hard drive from an online Python runner?

No. WebAssembly runs inside the browser security sandbox without access to host file system paths like C:\Windows. Use the Upload and Export buttons to load and save files through browser memory.

Which Python version does the browser runner execute?

The standard WebAssembly engine executes CPython 3.12.7 via Pyodide 0.26. The version selector also supports compatibility testing for Python 3.11 and Python 3.13 preview.

Test your Python scripts in the online runner

Fix syntax errors, inspect variable states, and verify code execution in our client-side WebAssembly workspace.

Launch Python Code Runner