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
python -m pdb script.py

# Debug a script from the command line.

Post-mortem debugging
import pdb
import traceback

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

# Inspect the traceback after an exception.

Disable breakpoint() globally
PYTHONBREAKPOINT=0 python app.py

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

Route breakpoint() to pdb.set_trace
PYTHONBREAKPOINT=pdb.set_trace python app.py

# Set an explicit hook from the environment.

## Breakpoints and Navigation
Break on a line number
b 42

# Set a line breakpoint in the current file.

Break on function entry
b module.function_name

# Stop when a function is entered.

Conditional breakpoint
b 42, user_id == 123

# Pause only when a condition is true.

Disable or enable a breakpoint
disable 1
enable 1

# Toggle an existing breakpoint by number.

Clear a breakpoint
clear 1

# Remove one breakpoint or all breakpoints.

Jump to another line
j 75

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

## Inspection and Frames
Show function arguments
a

# Display current frame arguments.

Inspect locals
p locals()

# Print local variables dictionary.

Inspect globals
p globals()

# Print global variables dictionary.

List full function source
ll

# Show the entire current function body.

Automatically display an expression
display user.balance

# Re-evaluate an expression each time execution stops.

Stop auto-displaying an expression
undisplay 1

# Remove one display entry.

## Advanced Usage
Run a statement under pdb
import pdb
pdb.run("main()")

# Debug an arbitrary statement string.

Debug a single function call
import pdb
pdb.runcall(func, arg1, arg2)

# Run a callable under debugger control.

Run with custom globals and locals
import pdb
pdb.runctx("result = fn(x)", globals(), locals())

# Control execution namespaces when debugging code strings.

Create a pdb alias
alias pl p locals()

# Define a custom shortcut command.

Recommended next

No recommendations yet.