Debugging & Profiling
pdb Debugger
Built-in Python debugger — interactive debugging with breakpoints, stepping, variable inspection, and post-mortem analysis.
Interview: Debugging skills — knowing how to use a debugger effectively is a fundamental development skill.
pdb (Python Debugger) is the built-in interactive debugger. It lets you pause execution, inspect variables, step through code line by line, and evaluate expressions. Python 3.7+ offers breakpoint() as a simpler alternative to import pdb; pdb.set_trace().
Starting the Debugger
breakpoint()— Python 3.7+ built-in (calls pdb.set_trace())import pdb; pdb.set_trace()— classic way to set a breakpointpython -m pdb script.py— debug from the start of a scriptpdb.pm()— post-mortem debugging after an unhandled exceptionpython -m pdb -c continue script.py— run until first breakpoint
Essential Commands
n(next) — execute current line, step over function callss(step) — step into function callsc(continue) — run until next breakpointl(list) — show code around current linep expr(print) — evaluate and print an expressionpp expr(pretty print) — formatted outputw(where) — show the call stacku/d(up/down) — navigate the call stackb line(break) — set a breakpoint at a line numberq(quit) — exit the debugger
Advanced Features
condition bpnum expr— conditional breakpoint (only stop when expr is True)commands bpnum— run commands automatically at a breakpointinteract— drop into a full Python REPL at the current frame!stmt— execute a Python statement (override pdb commands like 'n', 'c')alias name command— create shortcuts for common operations
Interview Insight
breakpoint() is the modern way to start debugging. Know the core commands: n (next), s (step into), c (continue), p (print), w (where). Post-mortem debugging (pdb.pm()) is invaluable for analyzing crashes without adding breakpoints.
Use Cases
Bug investigation — pausing at specific points to inspect state
Understanding unfamiliar code — stepping through execution flow
Post-mortem analysis — examining crash state after unhandled exceptions
Data inspection — examining complex data structures at runtime
Conditional debugging — stopping only when specific conditions occur
Common Mistakes
Leaving breakpoint() calls in production code — remove before deploying
Using print() instead of a debugger — pdb gives interactive inspection
Not using post-mortem debugging — pdb.pm() analyzes crashes without code changes
Confusing n (next) and s (step) — n steps over functions, s steps into them
Not knowing the interact command — it gives a full Python REPL at the current frame