As anywhere in programming, you'll only reach mastery with your debugger by using it a lot.
In this lesson, you first get to know the most important commands in pdb. After that, you'll see an example debugger session run on the short code snippet that you wrote earlier.
Most Common PDB Commands
Once you are in an interactive pdb session, you can navigate with the following commands and shortcuts:
These are new commands, and you'll need to practice them before you're comfortable and quick when working with the pdb debugger. That said, the basic commands shown above are definitely worth the learning effort.
Practice with Debugger Commands
- Recreate the same debugger session that you'll see below on your own computer.
- Explain what happens at each step of the example debugger terminal session shown below.
- Take out your notebook and write down all commands with a pen. Add your explanations and comments to each line.
Below is the output of the example interactive debugger session to consider:
>(venv) **➜ ****debugging** Python -m pdb debugging.py
> /Users/martin/<YOUR_PATH>/debugging.py(1)<module>()
-> a = 0
(Pdb) p a
*** NameError: name 'a' is not defined
(Pdb) n
> /Users/martin/<YOUR_PATH>/debugging.py(2)<module>()
-> b = 42
(Pdb) p a
0
(Pdb) p b
*** NameError: name 'b' is not defined
(Pdb) n
> /Users/martin/<YOUR_PATH>/debugging.py(4)<module>()
-> a += b
(Pdb) p b
42
(Pdb) n
--Return--
> /Users/martin/<YOUR_PATH>/debugging.py(4)<module>()->None
-> a += b
(Pdb) p a
42
(Pdb) p b
42
(Pdb) !a = 100
(Pdb) p a
100
(Pdb) p b
42
(Pdb) q
(venv) **➜ ****debugging
Run the example in your own interactive debugger and play around with what you can do. Consider the pdb commands from the documentation and experiment with some others that you find potentially useful.
Then, get yourself a piece of paper and a pencil and actually write it out manually. Add your comments and explanations. This is just a scratch paper for yourself, so it doesn't have to be pretty:
Writing by hand helps to fortify your memory, and explaining each line of code will help you better understand what pdb does and what possibilities you get by working with it.
In a future lesson, you'll get to know some other interactive debuggers that you can use with Python. Most of them are built on top of pdb and aim to improve the interface to make it visually more attractive. You'll be able to use the same commands in many other debuggers.
Additional Resources
- Stanford University: Common
pdbcommands pdbDocumentation: Debugger commands
Summary: Python, PDB Commands
- When your code execution hits a
breakpoint()function, Python enters an interactivepdbdebugger session that allows you to interact with the state of your script through the command line.
The Most Important Shortcut Commands
n: execute the current line of codep: print the variable's valuec: execute until a breakpoint is encountered!<expr>: execute Python statements as if in a normal interpreter sessionq: exit the Python debugger
Already, by learning just these couple of commands, you'll be able to investigate your program in a lot of detail without needing to write countless strategically placed print() functions all over your script.