Python comes with a built in debugger called pdb
. You drop the line below at any part or your code, and a debugger will show up:
import pdb; pdb.set_trace()
Python 3.7 introduced the debugger()
keyword, allowing you to simplify the code above and initiate a debugger with a single command:
breakpoint()
The breakpoint()
keyword isn't a new debugger, but a shortcut to initiate pdb
Using a Different Debugger
pdb
is simple and effective, but you might prefer a different debugger, like ipdb
. You can also use breakpoint()
to initiate your debugger of choice.
To do that, add an environment variable called PYTHONBREAKPOINT
passing in the command that sets the breakpoint:
$ export PYTHONBREAKPOINT=ipdb.set_trace
Now when the interpreter reaches the breakpoint()
call, ipdb
will initiate, instead of pdb
.
Make sure you have the chosen debugger installed.
Skipping the Breakpoints
Python allows disabling all the breaking points without changing your code. To do that, set PYTHONBREAKPOINT
to zero:
$ export PYTHONBREAKPOINT=0