PDB Cheat Sheet

In Python

PDB, or Python Debugger, is a powerful tool that allows developers to debug their Python code. It is a command-line tool that provides a way to step through code, set breakpoints, and inspect variables and objects at runtime.

PDB is built into Python, so there is no need to install any additional software. To use PDB, simply add the following line of code to your Python script:

import pdb; pdb.set_trace()

This will pause the execution of your code at the point where the `set_trace()` function is called, allowing you to interactively debug your code.

Once you have entered the PDB debugger, you can use a variety of commands to navigate through your code. Some of the most commonly used commands include:

  • `n` (next): Execute the current line and move to the next line.
  • `s` (step): Step into a function call.
  • `c` (continue): Continue execution until the next breakpoint is reached.
  • `p` (print): Print the value of a variable or expression.
  • `q` (quit): Quit the debugger.

PDB also provides a number of advanced features, such as the ability to set conditional breakpoints, examine the call stack, and interact with the Python interpreter.

Overall, PDB is an essential tool for any Python developer. By using PDB to debug your code, you can quickly identify and fix errors, saving time and improving the quality of your code.

This cheat sheet provides an overview of the most commonly used commands and features of the Python Debugger (PDB).

Starting PDB

CommandDescription
python -m pdb script.pyStart PDB and run script.py
import pdb; pdb.set_trace()Insert a breakpoint in your code

Basic Commands

CommandDescription
h or helpShow a list of available commands
q or quitQuit PDB
n or nextExecute the current line and move to the next line
s or stepExecute the current line and step into any function calls
c or continueContinue execution until the next breakpoint
r or returnContinue execution until the current function returns
l or listShow the current line and surrounding lines of code
w or whereShow the current stack trace
p or printEvaluate and print an expression
pp or pprintEvaluate and pretty-print an expression
a or argsShow the arguments of the current function
u or upMove up the stack trace
d or downMove down the stack trace

Breakpoints

CommandDescription
b or breakSet a breakpoint at the current line
b file.py:lineSet a breakpoint at file.py:line
b functionSet a breakpoint at the start of function
b function:lineSet a breakpoint at function:line
b with no argumentsList all breakpoints
cl or clearClear a breakpoint
cl file.py:lineClear the breakpoint at file.py:line
cl bpnumberClear the breakpoint with number bpnumber

Watchpoints

CommandDescription
watch expressionSet a watchpoint on expression
watch -r expressionSet a reverse watchpoint on expression
watch with no argumentsList all watchpoints
rwatch expressionSet a watchpoint on expression that only triggers when it is read
rwatch -r expressionSet a reverse watchpoint on expression that only triggers when it is read
rwatch with no argumentsList all reverse watchpoints
awatch expressionSet an automatic watchpoint on expression that is deleted after it triggers
awatch -r expressionSet a reverse automatic watchpoint on expression that is deleted after it triggers
awatch with no argumentsList all automatic watchpoints

Stepping

CommandDescription
s or stepStep into the current line
n or nextStep over the current line
unt or untilContinue execution until the current function returns or reaches a line greater than the current line
j or jumpJump to a different line in the current function

Stack Trace

CommandDescription
w or whereShow the current stack trace
u or upMove up the stack trace
d or downMove down the stack trace
bt or backtraceShow the full stack trace

Variables

CommandDescription
p or printPrint the value of a variable or expression
pp or pprintPretty-print the value of a variable or expression
a or argsShow the arguments of the current function
l or localsShow the local variables of the current function
g or globalsShow the global variables
whatis expressionShow the type of expression
source expressionShow the source code of expression
interactStart an interactive interpreter with access to the current frame

Miscellaneous

CommandDescription
h or helpShow a list of available commands
!Execute a Python statement in the current context
aliasDefine an alias for a command
unaliasRemove an alias
pdbrcLoad a configuration file
runRestart the program from the beginning

References