Sub topic 11
Reading errors and tracebacks
This chapter assumes you can run a script from the terminal (python3 hello.py) and that you have a working Python 3 installation. Here you learn to read the error report Python prints when a program fails, so you can find the file, the line, and the kind of problem.
What a traceback is
When Python hits an error it cannot recover from, it stops the program and prints a traceback. A traceback is a report of the chain of calls that led to the error. It is not a sign that Python is broken. It tells you where to look.
Save this file as broken.py:
print(mesage)Run it with python3 broken.py (on Windows, py broken.py). You will see output like this:
Traceback (most recent call last):
File "/home/ada/broken.py", line 1, in <module>
print(mesage)
NameError: name 'mesage' is not definedReading it from the bottom up
Start at the last line. It holds the two most useful facts.
- The error type, here
NameError. - The message, here
name 'mesage' is not defined.
Then read upward. Each File line gives a path and a line number. The indented line below it shows the code on that line. The text in <module> means the code ran at the top level of the file, not inside a function.
The first line, Traceback (most recent call last), tells you the order. The most recent call is at the bottom, next to the error.
Tracebacks through functions
When the error happens inside a function, the traceback has several File entries. Try this file:
def divide(a, b):
return a / b
def main():
print(divide(10, 0))
main()The output looks like this:
Traceback (most recent call last):
File "/home/ada/divide.py", line 9, in <module>
main()
File "/home/ada/divide.py", line 6, in main
print(divide(10, 0))
File "/home/ada/divide.py", line 2, in divide
return a / b
ZeroDivisionError: division by zeroRead from the bottom. The error happened on line 2, inside divide. The entry above shows who called divide (line 6, in main). The entry above that shows who called main. The bottom entry is where the error occurred. The entries above it show how the program got there.
Common error types
| Error type | Typical cause |
|---|---|
| `NameError` | A name is misspelled or used before it is defined |
| `SyntaxError` | The code breaks Python's grammar, such as a missing colon or quote |
| `IndentationError` | Lines are indented inconsistently |
| `TypeError` | An operation gets a value of the wrong type, such as `"a" + 1` |
| `ModuleNotFoundError` | An `import` names a package that is not installed in the active environment |
| `FileNotFoundError` | A file path is wrong, or the working directory is not what you expected |
| `ZeroDivisionError` | A number was divided by zero |
A practical routine
- Read the last line and name the error type.
- Find the lowest
Fileentry that points to your own file. Entries for installed packages are usually not the cause. - Open that line and compare the message with the code.
- Fix one thing, then run again.
Exercise
A traceback ends with ModuleNotFoundError: No module named 'requests'. Your virtual environment is not active. What is the likely cause, and what do you do?
Show answer
The requests package is not installed in the interpreter you used. Activate the virtual environment, then run python -m pip install requests and run the script again.
Exercise
In the divide.py traceback, which line number holds the actual failing operation, and which function contains it?
Show answer
Line 2, inside the divide function. It is the last File entry, directly above the error type line.