Sub topic 2
Starting a script under python -m pdb
This chapter assumes you can run Python scripts from a terminal, read a traceback, and set an environment variable in your shell. You already know breakpoint() from the previous chapter. Here you will start a debugger session without editing the source file.
Why run a script under pdb
breakpoint() needs a code change. Sometimes you cannot or do not want to change the file. The script may belong to someone else, or you may not yet know where to look. Running the script as python -m pdb solves this. Python loads the pdb module as the main program, and pdb then loads your script and stops before its first line runs.
python -m pdb script.pyArguments after the script name go to the script, so python -m pdb script.py --verbose data.csv works as you would expect.
A worked example
Save this file as script.py:
def mean(values):
total = sum(values)
return total / len(values)
def main():
data = []
print("mean:", mean(data))
main()Start it under pdb:
python -m pdb script.pyThe debugger stops on the first executable line and shows a prompt:
> /home/you/script.py(1)<module>()
-> def mean(values):
(Pdb)The > line names the file, the current line number in parentheses, and the current function. The <module> name means you are at the top level of the file. The -> line is the source line that runs next. It has not run yet.
At this point no function has been called. Type c (continue) to run until the program ends or crashes:
(Pdb) c
Traceback (most recent call last):
...
ZeroDivisionError: division by zero
Uncaught exception. Entering post mortem debugging
Running 'cont' or 'step' will restart the program
> /home/you/script.py(3)mean()
-> return total / len(values)
(Pdb)This is the main benefit of this mode. If the script raises an exception that nothing catches, pdb does not exit. It opens a post-mortem session on the frame where the error occurred. You can type p values and see [], which explains the failure. The next chapters cover post-mortem mode in detail.
Setting breakpoints from the prompt
Because you did not edit the file, you set breakpoints at the prompt. Use b with a line number or a function name, then c to run to it:
(Pdb) b mean
Breakpoint 1 at /home/you/script.py:2
(Pdb) c
> /home/you/script.py(2)mean()
-> total = sum(values)
(Pdb)Breakpoint management has its own chapter. For now, remember that b followed by c replaces adding a breakpoint() call.
Restarting and quitting
After a script finishes or crashes, pdb stays open. Typing c or s restarts the program from the beginning. Your breakpoints are kept across restarts, which makes it quick to repeat a run after you learn something. Type q to leave the debugger.
Choosing between the two entry points
| Situation | Better choice |
|---|---|
| You do not know where the bug is | `python -m pdb script.py` |
| The script may crash with an uncaught exception | `python -m pdb script.py` |
| You know the exact line and want to stop only there | `breakpoint()` |
| The code runs inside a server or a test runner | `breakpoint()` |
Exercise
You run python -m pdb script.py and the script raises an uncaught KeyError. What does pdb do, and which command lets you see the variables at the point of failure?
Show answer
pdb does not exit. It prints the traceback, announces post-mortem debugging, and stops at the frame where the exception was raised. You can then use p or pp locals() to look at the variables in that frame.