How to Read Error Messages
Beginners freeze when they see red text. Don't. Error messages tell you exactly what went wrong.
The Anatomy of a Python Error
Traceback (most recent call last):
File "/home/user/app.py", line 5, in <module>
result = divide(10, 0)
^^^^^^^^^^^^^
File "/home/user/app.py", line 2, in divide
return a / b
~~^~~
ZeroDivisionError: division by zero
Read it from bottom to top:
- Last line: The error type + message →
ZeroDivisionError: division by zero - Above that: The line that caused it →
return a / bon line 2 - Above that: The function call chain (traceback) →
divide(10, 0)on line 5
Common Errors and What They Mean
ModuleNotFoundError
ModuleNotFoundError: No module named 'flask'
Fix: Install the missing package:
pip install flask
Or activate your virtual environment if it's already installed there.
ImportError
ImportError: cannot import name 'jsonify' from 'flask'
Fix: You might have the wrong package name or version. Check the import statement and your installed version.
SyntaxError
File "script.py", line 3
print("hello"
^
SyntaxError: unexpected EOF while parsing
Fix: You're missing a closing parenthesis ) or bracket. Count your parentheses.
NameError
NameError: name 'app' is not defined
Fix: You used a variable before defining it. Check the spelling — did you mean app or App?
TypeError
TypeError: unsupported operand type(s) for +: 'int' and 'str'
Fix: You tried to add a number and a string. Convert one:
str(5) + " apples" # "5 apples"
5 + int("3") # 8
IndexError
IndexError: list index out of range
Fix: You tried to access an index that doesn't exist. Lists start at 0:
items = ["a", "b"]
items[0] # "a" — OK
items[2] # Error! Only indices 0 and 1 exist
KeyError
KeyError: 'username'
Fix: You tried to access a dictionary key that doesn't exist. Use .get() for safe access:
user = {"name": "Alice"}
user["name"] # "Alice" — OK
user.get("age") # None — safe
user["age"] # KeyError!
JSONDecodeError
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
Fix: You tried to parse an empty or non-JSON response. Check that the API endpoint exists and returns valid JSON. Print the raw response first:
print(response.text) # See what you're actually getting
ConnectionError / ConnectionRefusedError
ConnectionRefusedError: [Errno 61] Connection refused
Fix: You tried to connect to a server that isn't running. Start the server first, then run your client.
The 5-Step Debugging Process
- Read the last line first — What type of error is it?
- Find the file and line number — Where did it happen?
- Look at the line — Do you see the typo/mistake?
- Add print statements — Print variables to see what they contain
- Copy to Google — If stuck, copy the error message (without specific paths) into Google
Pro Tip: Print Everything
When something doesn't work, add print statements:
print(f"DEBUG: variable value is: {my_var}")
print(f"DEBUG: type is: {type(my_var)}")
print(f"DEBUG: response text is: {response.text}")
This is the single most effective debugging technique.