Debugging with pdb
Use Python's built-in debugger to step through code interactively.
Debugging with pdb is a free Python Academy lesson on CoddyKit. This is lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Python Academy learning path, and your progress syncs across the web and the CoddyKit app. The Python Academy course includes 4 lessons in total.
What Is pdb?
pdb is Python's built-in interactive debugger. It lets you pause execution, inspect variables, and step through code line by line.
import pdb
def faulty(n):
result = n * 2
pdb.set_trace() # execution pauses here
return result + 1
faulty(5)breakpoint() — Python 3.7+
breakpoint() is a built-in shorthand for import pdb; pdb.set_trace(). It respects the PYTHONBREAKPOINT env var.
def process(data):
for item in data:
if item < 0:
breakpoint() # drop into pdb here
print(item)
process([1, -2, 3])Core pdb Commands
Essential commands: n (next line), s (step into), c (continue), q (quit), p expr (print), l (list source).
# Inside pdb prompt:
# (Pdb) n — execute next line
# (Pdb) s — step into function
# (Pdb) c — continue to next breakpoint
# (Pdb) p my_var — print value
# (Pdb) l — show source around current line
# (Pdb) q — quitInspecting Variables
Use p to print an expression, pp for pretty-print, and locals() to see all local variables.
# (Pdb) p data
# [1, -2, 3]
# (Pdb) pp {"key": "value", "num": 42}
# {'key': 'value',
# 'num': 42}
# (Pdb) p locals()Conditional Breakpoints
Set conditional breakpoints to pause only when a condition is true, avoiding repeated stepping.
import pdb
def process(items):
for i, item in enumerate(items):
if item < 0: # manual condition
pdb.set_trace()
result = item * 2
# Or inside pdb: b filename.py:12, x < 0Post-Mortem Debugging
Inspect the state right after an unhandled exception using pdb.post_mortem() or run your script with python -m pdb script.py.
import pdb
try:
1 / 0
except ZeroDivisionError:
pdb.post_mortem() # inspect at the crash point
# Or from CLI:
# python -m pdb my_script.pyWatching Variables with w and where
w (where) prints the current call stack traceback so you know how you got to the current frame.
# (Pdb) w
# /app/main.py(10)main()
# -> process([1,-2,3])
# /app/main.py(5)process()
# -> breakpoint()Moving Between Frames
u (up) and d (down) move between call stack frames so you can inspect variables in calling functions.
# (Pdb) u — move to calling frame
# (Pdb) d — move back down
# (Pdb) p data — inspect variable in that frameipdb — Enhanced Debugger
ipdb wraps pdb with IPython features: tab completion, syntax highlighting, and magic commands.
# pip install ipdb
import ipdb
def calc(x):
ipdb.set_trace()
return x ** 2
calc(7)pudb — Visual Terminal Debugger
pudb provides a full-screen TUI debugger in the terminal with code, variable, and stack panels.
# pip install pudb
# python -m pudb my_script.py
import pudb
def run():
pudb.set_trace()
data = [1, 2, 3]
return sum(data)Disabling Breakpoints in CI
Set PYTHONBREAKPOINT=0 to make all breakpoint() calls no-ops in CI or production.
# In CI pipeline:
# export PYTHONBREAKPOINT=0
# python my_script.py
# breakpoint() is silently skippedQuick Check
What pdb command continues execution until the next breakpoint?
Recap
Use breakpoint() to pause execution and drop into pdb. Core commands: n, s, c, p, q. For richer experiences, try ipdb or pudb. Disable with PYTHONBREAKPOINT=0 in CI.
Learn Python with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 76
- Lessons
- 320
Frequently Asked Questions
Is the “Debugging with pdb” lesson free?
Yes — the full text of “Debugging with pdb” is free to read here on the web. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Python Academy course, upgrade to CoddyKit PRO. The Python Academy course includes 4 lessons in total.
What will I learn in “Debugging with pdb”?
Use Python's built-in debugger to step through code interactively. You practise Python Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Python Academy?
No prior experience is required. Python Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 4 of 4.
How long does the “Debugging with pdb” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Python Academy lesson?
Yes. Every Python Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.