On this page
pdb
Prev: pathlib | Up: stdlib_module | Next: pickle
Rel: typing, annotationlib, pydoc, pydoc_data, doctest
Ref: docs.python.org — pdb
Tags: #public #python3 #stdlib
Import
import pdb
Overview
Python's built-in interactive source debugger (breakpoint(), pdb.set_trace(), pdb.post_mortem(), python -m pdb script.py). Use built-in breakpoint() in code (which delegates to pdb by default and honors PYTHONBREAKPOINT=0 to disable breakpoints in CI/production).
Quick Start
# Run a script under pdb from line 1, or drop into post-mortem on uncaught error:
python3 -m pdb my_script.py
python3 -m pdb -c continue my_script.py
Implementation Walkthrough
1. Essential pdb Interactive Commands
n(next): Step over the current line.s(step): Step into a called function.c(continue): Resume until the next breakpoint.l/ll(list): Show surrounding source lines / full current function.p expr/pp expr: Print or pretty-print an expression.w(where): Print the current call stack;u/dmove up/down frames.q(quit): Abort debugging immediately.
2. Post-Mortem Debugging on Failure (pdb.post_mortem)
import pdb
def debug_on_crash(fn):
try:
return fn()
except Exception:
pdb.post_mortem()
raise
Gotchas & Boundaries
- Prefer built-in
breakpoint()overimport pdb; pdb.set_trace():breakpoint()can be globally disabled withPYTHONBREAKPOINT=0or redirected to another debugger. - Remove
breakpoint()calls before committing: A stray breakpoint hangs headless CI jobs waiting for stdin. - Prefix variable names that collide with
pdbcommands (n,c,l,p) with!(e.g.,!c = 5orp c).
Common Functions
- pdb.get_default_backend() —
get_default_backend()— Get the default backend to use for Pdb instances. - pdb.help() —
help()—pdb.help()function. - pdb.pm() —
pm()— Enter post-mortem debugging of the traceback found in sys.last_exc. - pdb.post_mortem() —
post_mortem(t=None)— Enter post-mortem debugging of the given traceback, or exception object. - pdb.run() —
run(statement, globals=None, locals=None)— Execute the statement (given as a string or a code object) under debugger control. - pdb.runcall() —
runcall(*args, **kwds)— Call the function (a function or method object, not a string) with the given arguments. - pdb.runctx() —
runctx(statement, globals, locals)—pdb.runctx()function. - pdb.runeval() —
runeval(expression, globals=None, locals=None)— Evaluate the expression (given as a string or a code object) under debugger control. - pdb.set_default_backend() —
set_default_backend(backend)— Set the default backend to use for Pdb instances. - pdb.set_trace() —
set_trace(*, header=None, commands=None)— Enter the debugger at the calling stack frame.
Classes & Objects
- pdb.Pdb —
pdb.Pdbclass. (Methods:.bp_commands(),.break_anywhere(),.break_here(),.canonic(),.checkline(),.clear_all_breaks())
Linked from
- tuts/python3/stdlib_module
- tuts/python3/stdlib_module/bdb
- tuts/python3/stdlib_module/code
- tuts/python3/stdlib_module/linecache
- tuts/python3/stdlib_module/pathlib
- tuts/python3/stdlib_module/pickle
- tuts/python3/stdlib_module/stdlib_module
- tuts/python3/tutorials/intermediate_python_and_software_engineering/testing_and_debugging
Related notes
- tuts/python3/stdlib_module/pathlib Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/pickle Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/stdlib_module Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/annotationlib Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/bdb links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/code links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/doctest Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/linecache links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/pdb/get_default_backend Linked here; shared tags: #stdlib