pdb

pdb is the Python debugger included in Python’s standard library. It provides an interactive, text-based debugging environment for Python programs that allows stepping through code execution, inspecting variables, and analyzing program state.

Core Features

Post-mortem Debugging

  • Launches debugger when unhandled exceptions occur
  • Allows inspection of program state at point of failure

Interactive Debugging

  • Steps through code line by line
  • Sets breakpoints at specific lines
  • Evaluates expressions in current context
  • Modifies variables during execution

Integration Methods

  • Command line with python -m pdb script.py
  • Hard-coded breakpoints via breakpoint() function
  • Post-mortem analysis after exceptions
  • Remote debugging capabilities

Key Commands

Program Flow

  • n (next): Execute next line
  • s (step): Step into function call
  • c (continue): Continue execution
  • r (return): Continue until current function returns
  • q (quit): Exit debugger

Breakpoints

  • b [filename:]lineno: Set breakpoint
  • clear [filename:]lineno: Remove breakpoint
  • tbreak: Set temporary breakpoint
  • disable: Disable breakpoint
  • enable: Enable breakpoint

Inspection

  • p expression: Print value of expression
  • pp expression: Pretty-print value
  • whatis expression: Print type of expression
  • l: List source code around current line
  • a: Print current function’s arguments

Limitations

  • Basic text-based interface
  • Limited IDE integration
  • Cannot modify frozen code objects

Learn More

Last updated on

Please submit corrections and feedback...