ReviseAlgo Logo

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.

Last Updated: June 12, 2026 8 min read

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 breakpoint
  • python -m pdb script.py — debug from the start of a script
  • pdb.pm() — post-mortem debugging after an unhandled exception
  • python -m pdb -c continue script.py — run until first breakpoint

Essential Commands

  • n (next) — execute current line, step over function calls
  • s (step) — step into function calls
  • c (continue) — run until next breakpoint
  • l (list) — show code around current line
  • p expr (print) — evaluate and print an expression
  • pp expr (pretty print) — formatted output
  • w (where) — show the call stack
  • u / d (up/down) — navigate the call stack
  • b line (break) — set a breakpoint at a line number
  • q (quit) — exit the debugger

Advanced Features

  • condition bpnum expr — conditional breakpoint (only stop when expr is True)
  • commands bpnum — run commands automatically at a breakpoint
  • interact — 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