Using pdb: essential commands and pytest --pdb

Backend Live Coding November 13, 2026 bash

pytest --pdb is the most useful: it drops you into the failing frame with all the local variables.

bash
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
# ======================================================================
# Q: How do you use pdb? Give a few commands.
#
# pytest --pdb is the most useful: it drops you into the failing frame with all
# the local variables. In production I never do interactive debugging; there I
# work with logs, Sentry and the tools that follow.
# ======================================================================

def apply_discount(order):
    breakpoint()                  # PYTHONBREAKPOINT=0 disables it

(Pdb) p order.total, order.customer.discount_rate
(Pdb) pp vars(order)
(Pdb) n / s / c               # next, step into, continue
(Pdb) w                       # stack trace: who called me
(Pdb) u / d                   # move up/down the stack
(Pdb) b pricing.py:42, qty > 100   # conditional breakpoint
(Pdb) interact                # full Python REPL in the current frame

# post-mortem on a failing test
pytest tests/test_pricing.py -x --pdb