Sub topic 1
Pausing your program with breakpoint()
Prerequisites
This chapter assumes you can run Python scripts from a terminal, write functions and handle exceptions, read a traceback, and set an environment variable in your shell. You need Python 3.7 or later, because breakpoint() was added in 3.7.
What breakpoint() does
breakpoint() is a built-in function that pauses your program at the line where you call it and opens the debugger prompt. By default it starts pdb, the debugger in Python's standard library. The prompt looks like (Pdb). While the program is paused, you can look at variable values and run code in the current scope.
Before Python 3.7 you had to write import pdb; pdb.set_trace(). breakpoint() does the same thing with less typing. It also lets you change or turn off the debugger without editing the source, which a later chapter covers.
A first session
The function below fails when it receives an empty list. You can see the ZeroDivisionError in the traceback, but not the values that caused it. Put breakpoint() just before the line you suspect.
def mean(values):
total = sum(values)
breakpoint()
return total / len(values)
print(mean([]))Save this as mean.py and run python mean.py. Execution stops before the return line, and you see something like this:
> /home/you/mean.py(4)mean()
-> return total / len(values)
(Pdb)The first line gives the file, the line number in parentheses, and the function name. The line starting with -> is the next line to run. It has not run yet. Now inspect the state:
(Pdb) p total
0
(Pdb) p len(values)
0
(Pdb) cp prints the value of an expression. Here len(values) is 0, so the division must fail. c (continue) resumes the program, and the traceback then appears as usual.
Where to place it
Place breakpoint() on its own line, at the same indentation as the code around it. The program stops before the next statement runs, so the variables you inspect reflect everything above the call.
Some common placements:
- Before the line that raises an exception, as in the example above.
- At the top of a function, to check the arguments it received.
- Inside a loop body, to examine one iteration. Be careful: the debugger stops on every pass, so guard it with a condition such as
if i == 50: breakpoint(). - Inside an
iforelsebranch, to confirm that the branch is reached at all.
Common errors
- Leaving
breakpoint()in committed code. A program that reaches it in production or in a scheduled job will wait for input forever. Search for it before you commit. - Expecting the current line to have run. The
->line is pending. Variables it assigns do not exist yet. - Using it where there is no terminal. Under a web server, a service manager, or a container without an attached terminal, the prompt has nowhere to read input from, and the process may hang or fail.
- Typing a variable name that matches a pdb command. If your variable is named
c,n, orl, typing it runs the command. Usep cto print the variable.
Turning it off
Setting the environment variable PYTHONBREAKPOINT=0 makes every breakpoint() call do nothing, so you can run the script unchanged:
PYTHONBREAKPOINT=0 python mean.pyThe traceback appears at once, with no pause. The same variable can select a different debugger, and a later chapter covers that.
Exercise
You add breakpoint() on line 3 of a function, and line 3 is result = compute(x). At the (Pdb) prompt you type p result. What happens, and why?
Show answer
Pdb reports a NameError (or shows an older value if result was already assigned earlier). The program stops before line 3 runs, so compute(x) has not been called and result has not been assigned this time. Step forward with n to run the line, then print result again.
Exercise
How can you run a script that contains breakpoint() calls without any pause, and without editing the file?
Show answer
Set the environment variable PYTHONBREAKPOINT to 0 for that run, for example PYTHONBREAKPOINT=0 python script.py.