Sub topic 10
Controlling breakpoint() with PYTHONBREAKPOINT
This chapter assumes you can run Python scripts from a terminal, set environment variables in your shell, and use breakpoint() to open the (Pdb) prompt.
What breakpoint() actually calls
The breakpoint() built-in does not contain a debugger. It calls sys.breakpointhook(). By default, that hook reads the environment variable PYTHONBREAKPOINT and decides what to run. If the variable is unset, the hook calls pdb.set_trace().
This gives you a switch you can flip without editing the source. You can turn every breakpoint() call off, or send them to a different debugger.
def mean(values):
total = sum(values)
breakpoint()
return total / len(values)
print(mean([1, 2, 3]))You can run this one file in three ways, with no code changes.
The values PYTHONBREAKPOINT accepts
| Value | Effect |
|---|---|
| unset or empty | Calls `pdb.set_trace()`, the default |
| `0` | Every `breakpoint()` call does nothing |
| `pdb.set_trace` | Same as the default, written out |
| `module.function` | Imports `module` and calls `function` |
Any dotted name works if Python can import the module part. For example, PYTHONBREAKPOINT=pdb.set_trace names the default function explicitly.
python script.py
PYTHONBREAKPOINT=0 python script.py
PYTHONBREAKPOINT=pdb.set_trace python script.pyThe first command stops at the prompt. The second runs straight through, so you can leave breakpoint() calls in place while testing a fix or running a script in a pipeline. The third behaves like the first.
Switching to another debugger
Third-party debuggers often provide a function you can name. If ipdb is installed, this uses it instead of pdb:
PYTHONBREAKPOINT=ipdb.set_trace python script.pyYour source stays the same. Only the environment changes, so the choice of debugger lives with each developer or each machine, not in the code.
Arguments pass through
Anything you pass to breakpoint() is forwarded to the hook. The default pdb.set_trace() takes no extra arguments, but a custom hook can. The example below defines a hook that prints a message and then opens pdb.
# myhook.py
import pdb
def announce(*args, **kwargs):
print("Entering debugger", args, kwargs)
pdb.set_trace(*args, **kwargs)PYTHONBREAKPOINT=myhook.announce python script.pyRun this from the directory that contains myhook.py, so that Python can import it.
Common errors
- Setting the variable after Python starts. The hook reads the variable each time
breakpoint()is called, so changingos.environinside the program also works. Changing it in another terminal does not. - A name that cannot be imported. If the module or function is missing, Python emits a
RuntimeWarningandbreakpoint()does nothing. Read the warning rather than assuming the code ran past a stop. - Leaving
breakpoint()in shipped code. SettingPYTHONBREAKPOINT=0is a safety net. Still remove the calls before release, because a missing variable in production would stop the process at a prompt.
How the pieces connect
Exercise
A teammate's script contains several breakpoint() calls. You want to run it in a batch job without stopping, and without editing the file. What command do you use, and which debugger entry points would still stop the program?
Show answer
Run PYTHONBREAKPOINT=0 python script.py. It makes every breakpoint() call a no-op. Direct calls such as pdb.settrace() and pdb.postmortem(), and sessions started with python -m pdb, are not affected and would still open the debugger.
Exercise
You run PYTHONBREAKPOINT=mytools.debug python script.py, but the program runs through without stopping and prints a RuntimeWarning. What is the most likely cause?
Show answer
Python could not import the module mytools or find the function debug in it, so breakpoint() did nothing. Check the spelling, that the module is on the import path (for example, in the current directory), and that the function exists.