Reading Error Messages¶
Error messages are Python's way of telling you exactly what went wrong and where. This guide teaches you how to decode them.
Learn Your Way¶
| Read | Build | Watch | Test | Review | Visualize |
|---|---|---|---|---|---|
| You are here | Projects | Videos | Quiz | Flashcards | Diagrams |
Anatomy of a traceback¶
Traceback (most recent call last):
File "app.py", line 12, in main
result = process(data)
File "app.py", line 7, in process
return data["name"].upper()
TypeError: 'NoneType' object has no attribute 'upper'
Read a traceback bottom to top:
- Last line -- the error type and message:
TypeError: 'NoneType' object has no attribute 'upper' - Lines above -- the call stack, showing each function that was called. The bottom frame is where it actually broke.
- File and line number -- tells you exactly where to look:
File "app.py", line 7 - Code snippet -- the exact line that failed:
return data["name"].upper()
The call stack shows the path Python took to reach the error. In this example: main() called process(), and process() broke on line 7.
The 10 most common errors¶
1. SyntaxError -- Python cannot parse your code¶
What it means: Your code has a grammar mistake. Python could not even begin running it. Common causes: Missing colons, unmatched parentheses/quotes, typos in keywords.
2. NameError -- a name does not exist¶
What it means: You used a name Python has never seen. It does not exist in the current scope. Common causes: Typos in variable names, using a variable before defining it, forgetting an import.
3. TypeError -- wrong type for the operation¶
What it means: You tried to do something with the wrong type of value. Common causes: Adding strings and numbers, calling something that is not a function, wrong number of arguments.
4. ValueError -- right type, wrong value¶
What it means: The type is correct (it is a string), but the specific value cannot be used. Common causes: Converting non-numeric strings to numbers, unpacking the wrong number of values.
5. IndexError -- list index out of range¶
What it means: You asked for a position that does not exist in the list. Common causes: Off-by-one errors, looping past the end, empty lists.
6. KeyError -- dict key does not exist¶
What it means: You asked for a dictionary key that is not there.
Fix: Use .get("email") for safe access, or check with if "email" in person: first.
7. AttributeError -- object does not have that attribute¶
What it means: You tried to access a method or property that does not exist on this type.
Common causes: Calling methods on None (a function returned nothing), typos in method names.
8. ImportError / ModuleNotFoundError -- cannot find the module¶
What it means: Python cannot find the module you are trying to import.
Common causes: Package not installed (pip install pandas), wrong virtual environment, file named the same as a module.
9. FileNotFoundError -- file does not exist¶
What it means: The file path you gave does not point to an existing file. Common causes: Wrong filename, wrong directory, typo in the path.
10. IndentationError -- inconsistent indentation¶
What it means: Python expected indented code but found something at the wrong level.
Common causes: Missing indentation after if/for/def/class, mixing tabs and spaces.
How to read each part of a traceback¶
Traceback (most recent call last): ← Start of the call chain
File "main.py", line 15, in <module> ← Which file, which line, which scope
run() ← The code on that line
File "main.py", line 10, in run ← One level deeper
total = add_prices(items) ← The code that called the next level
File "main.py", line 5, in add_prices ← Where it actually broke
total += item["price"] ← The exact failing line
KeyError: 'price' ← What went wrong (THE ANSWER)
Step-by-step reading process:
- Look at the very last line -- this is the error type and message
- Look at the frame just above -- this is where the error actually happened
- Note the file name and line number -- open your file and go to that line
- Read the code snippet -- does the code match what you expected?
- If needed, trace upward through the call stack to understand how you got there
The friendly library¶
The friendly library rewrites Python error messages in plain English:
It turns cryptic messages into explanations like:
A NameError exception indicates that a variable or function name
is not known to Python. Most often, this is because there is a
spelling mistake. However, sometimes it is because the name is
used before being defined or given a value.
Did you mean 'username'?
This is especially helpful while you are still learning to read tracebacks on your own.
Practice¶
- Level 00 / 08 Making Decisions
- Level 00 / 15 Putting It Together
- Level 0 / 07 First File Reader
- Level 1 / 01 Input Validator Lab
- Level 2 / 04 Error Safe Divider
- Level 2 / 11 Retry Loop Practice
Quick check: Take the quiz
Review: Flashcard decks Practice reps: Coding challenges
| ← Prev | Home | Next → |
|---|---|---|