Python pdb and Breakpoint Cheat Sheet

Focused reference for pdb commands, breakpoints, stepping, stack frames, and conditional debugging.

View
StandardDetailedCompact
Export
Copy the compact sheet, download it, or print it.
Download
`D` dense toggle · `C` copy all

Starting the Debugger

Start pdb from CLI

Debug a script from the command line.

bashANYpdbcli
bash
python -m pdb script.py

Post-mortem debugging

Inspect the traceback after an exception.

pythonANYpdbpostmortem
python
import pdb
import traceback

try:
    main()
except Exception:
    traceback.print_exc()
    pdb.post_mortem()

Disable breakpoint() globally

Make breakpoint() a no-op during a run.

bashANYbreakpointenvironment
bash
PYTHONBREAKPOINT=0 python app.py

Route breakpoint() to pdb.set_trace

Set an explicit hook from the environment.

bashANYbreakpointpdb
bash
PYTHONBREAKPOINT=pdb.set_trace python app.py

Breakpoints and Navigation

Break on a line number

Set a line breakpoint in the current file.

textANYbreakpoints
text
b 42

Break on function entry

Stop when a function is entered.

textANYbreakpointsfunctions
text
b module.function_name

Conditional breakpoint

Pause only when a condition is true.

textANYbreakpointsconditional
text
b 42, user_id == 123

Disable or enable a breakpoint

Toggle an existing breakpoint by number.

textANYbreakpoints
text
disable 1
enable 1

Clear a breakpoint

Remove one breakpoint or all breakpoints.

textANYbreakpointscleanup
text
clear 1

Jump to another line

Change the next line to be executed in the current frame.

textANYcontrol-flowdanger
text
j 75

Inspection and Frames

Show function arguments

Display current frame arguments.

textANYframesarguments
text
a

Inspect locals

Print local variables dictionary.

textANYlocalsvariables
text
p locals()

Inspect globals

Print global variables dictionary.

textANYglobalsvariables
text
p globals()

List full function source

Show the entire current function body.

textANYsource-view
text
ll

Automatically display an expression

Re-evaluate an expression each time execution stops.

textANYwatchinspection
text
display user.balance

Stop auto-displaying an expression

Remove one display entry.

textANYwatch
text
undisplay 1

Advanced Usage

Run a statement under pdb

Debug an arbitrary statement string.

pythonANYpdbembedded
python
import pdb
pdb.run("main()")

Debug a single function call

Run a callable under debugger control.

pythonANYpdbfunctions
python
import pdb
pdb.runcall(func, arg1, arg2)

Run with custom globals and locals

Control execution namespaces when debugging code strings.

pythonANYpdbcontexts
python
import pdb
pdb.runctx("result = fn(x)", globals(), locals())

Create a pdb alias

Define a custom shortcut command.

textANYpdbproductivity
text
alias pl p locals()

Recommended next

No recommendations yet.