Sub topic 3
Reading the (Pdb) prompt and getting help
This chapter assumes you can run Python scripts from a terminal, read a traceback, and have already started a pdb session with breakpoint() or python -m pdb. Here you learn what the prompt shows you and how to find commands without leaving the debugger.
What the prompt tells you
When pdb stops, it prints two lines and then waits for input. Take this script:
def mean(values):
total = sum(values)
breakpoint()
return total / len(values)
print(mean([]))Running it produces output like this:
> /home/you/mean.py(4)mean()
-> return total / len(values)
(Pdb)Each part has a meaning:
> /home/you/mean.py(4)mean()is the current frame. It gives the file, the line number in parentheses, and the function name.-> return total / len(values)is the line that is about to run. It has not run yet.(Pdb)is the prompt. Type a command and press Enter.
The line marked with -> is a common source of confusion. pdb stops before executing that line, so any assignment on it has not happened. Here the breakpoint is on line 3, but pdb shows line 4 because breakpoint() itself has already finished.
Command forms
Most commands have a short alias and a full name. p and print do the same thing, as do n and next. This tutorial uses the short forms because they are faster to type.
Two habits are worth knowing:
- Pressing Enter on an empty line repeats the previous command. This is handy when you step with
nmany times. - If you type something that is not a pdb command, pdb evaluates it as Python in the current frame. Typing
totalshows its value, andx = 5changes a variable.
That second habit has a trap. If a variable has the same name as a command, pdb runs the command. A variable called c or n cannot be shown by typing its name. Use p c instead.
Getting help
The help command lists every command pdb knows:
(Pdb) help
Documented commands (type help <topic>):
========================================
EOF c d h list q rv undisplay
a cl debug help ll quit s unt
alias clear disable ignore longlist r source until
...Pass a command name to read its description:
(Pdb) help until
(Pdb) h bThe list varies slightly between Python versions, so check your own interpreter instead of relying on a table from a book.
Two related commands are useful early on:
hwith no argument shows the command list, the same ashelp.q(orquit) ends the debugger and stops the program. Usecinstead if you want the program to keep running.
Confirming where you are
When you lose track of your position, two commands reorient you:
(Pdb) l
(Pdb) wl (list) prints about eleven lines of source around the current line, with -> marking the next line to run. w (where) prints the call stack. Both are covered in detail in later chapters. For now, treat them as the way to answer "where am I?"
Common errors
- Typing a variable named like a command, such as
n, and getting a step instead of a value. Usep n. - Expecting the
->line to have already run. - Pressing
qwhen you meantc, which kills the program. - Pasting a multi-line block into the prompt. pdb reads one line at a time, so use
!followed by a single statement, or define helpers in the code itself.
Exercise
A script stops with -> total = a + b shown under the (Pdb) banner. You type p total and get a NameError. Why, and which commands would you use to move forward and then check the value?
Show answer
The arrow marks the line that is about to run, so total has not been assigned yet. Type n to execute the line, then p total to see the value. If you are unsure of your position, use l first, and use help n to read what n does.