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
Command
Description
python -m pdb script.py
Start PDB and run script.py
import pdb; pdb.set_trace()
Insert a breakpoint in your code
Basic Commands
Command
Description
h or help
Show a list of available commands
q or quit
Quit PDB
n or next
Execute the current line and move to the next line
s or step
Execute the current line and step into any function calls
c or continue
Continue execution until the next breakpoint
r or return
Continue execution until the current function returns
l or list
Show the current line and surrounding lines of code
w or where
Show the current stack trace
p or print
Evaluate and print an expression
pp or pprint
Evaluate and pretty-print an expression
a or args
Show the arguments of the current function
u or up
Move up the stack trace
d or down
Move down the stack trace
Breakpoints
Command
Description
b or break
Set a breakpoint at the current line
b file.py:line
Set a breakpoint at file.py:line
b function
Set a breakpoint at the start of function
b function:line
Set a breakpoint at function:line
b with no arguments
List all breakpoints
cl or clear
Clear a breakpoint
cl file.py:line
Clear the breakpoint at file.py:line
cl bpnumber
Clear the breakpoint with number bpnumber
Watchpoints
Command
Description
watch expression
Set a watchpoint on expression
watch -r expression
Set a reverse watchpoint on expression
watch with no arguments
List all watchpoints
rwatch expression
Set a watchpoint on expression that only triggers when it is read
rwatch -r expression
Set a reverse watchpoint on expression that only triggers when it is read
rwatch with no arguments
List all reverse watchpoints
awatch expression
Set an automatic watchpoint on expression that is deleted after it triggers
awatch -r expression
Set a reverse automatic watchpoint on expression that is deleted after it triggers
awatch with no arguments
List all automatic watchpoints
Stepping
Command
Description
s or step
Step into the current line
n or next
Step over the current line
unt or until
Continue execution until the current function returns or reaches a line greater than the current line
j or jump
Jump to a different line in the current function
Stack Trace
Command
Description
w or where
Show the current stack trace
u or up
Move up the stack trace
d or down
Move down the stack trace
bt or backtrace
Show the full stack trace
Variables
Command
Description
p or print
Print the value of a variable or expression
pp or pprint
Pretty-print the value of a variable or expression
a or args
Show the arguments of the current function
l or locals
Show the local variables of the current function
g or globals
Show the global variables
whatis expression
Show the type of expression
source expression
Show the source code of expression
interact
Start an interactive interpreter with access to the current frame