Understanding error messages is half the battle in debugging. This reference explains the most common ones.

SyntaxError

Cause: Invalid Python syntax — the parser can’t understand your code.

  # Missing colon
if x > 0
    print(x)

# Missing parenthesis
print "hello"

# Invalid assignment
def func()
    pass
  

Fix: Read the caret (^) in the traceback — it points to the problem line. Check colons, parentheses, quotes, and indentation.


IndentationError

Cause: Inconsistent indentation (mixing tabs and spaces, wrong level).

  def func():
print("wrong indent")
  

Fix: Use 4 spaces consistently. Configure your editor to insert spaces, not tabs.


NameError

Cause: Using a variable or function before it’s defined.

  print(undefined_var)
prnt("typo")  # typo in function name
  

Fix: Check spelling. Ensure the variable is assigned before use. Watch for scope issues (variable defined inside a function but used outside).


TypeError

Cause: Operation on wrong type.

  "2" + 2              # can't concatenate str and int
len(42)              # int has no length
func(1, 2, 3)        # func only accepts 2 args
  

Fix: Check types with type(x). Convert explicitly: int("42"), str(42). Read the function signature.


ValueError

Cause: Right type, wrong value.

  int("abc")           # string isn't a number
[1, 2, 3].index(99)  # value not in list
  

Fix: Validate input before conversion. Use try/except for user input.


IndexError

Cause: List/tuple index out of range.

  lst = [1, 2, 3]
lst[10]
lst[-10]
  

Fix: Check len(lst) before indexing. Use .get() for safe access patterns.


KeyError

Cause: Dictionary key doesn’t exist.

  d = {"a": 1}
d["b"]
  

Fix: Use d.get("b", default) instead of d["b"]. Check with "b" in d.


AttributeError

Cause: Object doesn’t have the attribute or method.

  "hello".append("!")  # strings don't have append
None.upper()         # NoneType has no upper
  

Fix: Check the object type. Verify the attribute name. Ensure the object isn’t None.


ImportError / ModuleNotFoundError

Cause: Module not installed or wrong import path.

  import nonexistent_module
from myapp import missing_function
  

Fix:

  pip install package_name
  

Check virtual environment is activated. Verify file structure and __init__.py files.


ZeroDivisionError

  10 / 0
  

Fix: Check divisor before dividing: if b != 0.


FileNotFoundError

  open("missing.txt")
Path("missing.txt").read_text()
  

Fix: Check path with Path.exists(). Use absolute paths or verify working directory.


RecursionError

Cause: Function calls itself too many times (infinite recursion).

  def recurse():
    return recurse()
  

Fix: Ensure base case in recursive functions. Increase limit only as last resort: sys.setrecursionlimit().


AssertionError

  assert x > 0, "x must be positive"
  

Fix: The condition was False. Fix the logic or the input. Don’t use assert for user input validation in production.


Debugging Strategy

  1. Read the traceback bottom-up — last line is the error type and message
  2. Find your code in the traceback (not library code)
  3. Print variable values at the failing line
  4. Use breakpoint() for interactive inspection
  5. Search the error message — someone has hit this before

See Error Handling for try/except patterns and Debugging techniques.