Interpreting
Python Tracebacks
Learn to read Python's error messages like a detective: find the line number, identify the exception type, and fix bugs faster.
What Is a Traceback?
When your Python program crashes, it doesn't just say "something went wrong." Instead, it prints a traceback: a detailed report that shows exactly where the error happened and what kind of error it was.
Every traceback contains three essential pieces of information: the call stack (the path), the line number (the location), and the exception type + message (the cause).
Anatomy of a Traceback
Why Tracebacks Matter
The Golden Rule: Read Bottom-Up
Syntax vs. Runtime Errors
| SyntaxError | Runtime Errors | |
|---|---|---|
| When? | Before code runs | While code runs |
| Traceback? | Short: file + line + caret (^) | Full call stack |
| Example | Missing colon, bad indent | NameError, TypeError |
| Call stack? | No (parsing failed) | Yes (shows function chain) |
Common Exception Types
More Exception Types
How to Read a Traceback
Example 1: NameError
Example 2: TypeError
Example 3: Multi-Level Call Stack
The Caret Indicator (^)
Starting with Python 3.10, tracebacks include caret indicators (^ and ~) that pinpoint the exact expression inside the line that caused the error.
SyntaxError: A Special Case
A SyntaxError has a different format from runtime tracebacks. There is no call stack because the code never started running.
Common Mistakes When Reading Tracebacks
Quick Reference Table
| Exception | Meaning | Typical Fix |
|---|---|---|
| NameError | Undefined variable/function | Check spelling, define first |
| TypeError | Wrong type in operation | Cast types or fix arguments |
| IndexError | List index out of range | Check list length first |
| ValueError | Right type, wrong value | Validate input before use |
| KeyError | Dict key not found | Use .get() or check key |
| SyntaxError | Invalid Python syntax | Fix colons, brackets, indentation |
Question 1 of 5
When reading a Python traceback, where should you start reading?
Question 2 of 5
Given the traceback above, on which line did the error occur, and what is the exception type?
Question 3 of 5
What does a SyntaxError traceback look like compared to a runtime traceback?
Question 4 of 5
This traceback shows a KeyError. What does that tell you?
Question 5 of 5
This code crashes. What exception type will appear in the traceback, and on which line?