Part of the Beginner's Guide series:
Cheat sheet for debugging python code with pdb.
Start pdb
python1import pdb
2pdb.set_trace()
This command will create a breakpoint in your code. When the code reaches this point, it will stop and you will be able to debug it. The cmd line will look like this:
bash1(Pdb) # here you can type commands, or check the value of variables
pdb commands
Check the source code
bash1(Pdb) l # list 11 lines of code around the current line
1(Pdb) ll # list the whole function
Step next
Command | Description |
---|---|
n |
step next (will not go into functions) |
s |
step into (will go into functions) |
r |
step out (will go out of functions) |
Check variables
bash1(Pdb) p <variable> # print the value of a variable
Actually, you can use any python code here, for example:
bash1(Pdb) p [x for x in range(10)] # print a list
p
is not necessary, you can just type the code and it will be executed.
Other commands
Command | Description |
---|---|
c |
continue execution (will stop at the next breakpoint) |
q |
quit the debugger (will stop the execution) |
a |
print the argument list of the current function |
whatis <variable> |
print the type of a variable |