Lesson Three: Using the Python Debugger

A debugger will let you examine your program as it is running. You can step through each line in your code, one by one, and see what each statement does. You can make sure that every statement has the expected outcome and that your variables contain the right values. Debuggers are great tools for finding and fixing runtime problems!

To use a debugger, your program must be able to run without any syntax errors. Use other tools to find and fix syntax errors before you try to find runtime issues with a debugger.
Program States - Running and Break
Microscope examining line of codeNormally, your program will start at the beginning and run at full speed, pausing only when prompting the user for input. In this "running state", the full-speed execution is so fast, you would never be able to closely watch each statement and see what happens. You also don't have the opportunity to look at your variables to see what values are actually inside.

A debugger allows you to put your program into a "break state". When in break, your program is paused temporarily so you can look at what is going on inside. Your program will be paused at a statement that is about to be run. This break state allows you to check the contents of your variables and make sure the data looks good. You can also watch the program flow

Loading the Python Debugger
The Python debugger is not enabled by default. It is kept in a separate library called "pdb" (short for "Python Debugger"). To bring the pdb library into your program, you need to add an import statement at the top of your code. "import" tells Python to bring in some additional tools or code that has been pre-written elsewhere. To enable the Python debugger, simply add "import pdb" as the first line in your code.

The example below re-creates a program from the last lesson. This tax-calculation program will calculate a final bill given a starting price, tax rate, and tip. It contains two run-time errors that keep it from giving the correct output. We've added the "import pdb" statement at the top so we can examine this program in the debugger.

import pdb # enable Python debugger

price = 10.00
taxRate = 0.07
tax = price * taxRate # calculate the tax
total = taxRate + tax # add the tax to the price
tip = 2.00
if (tip < 0): # if there is a tip
total = total + tip # add the tip to the bill
print("Your total bill is: $",total)
Copy
Setting Breakpoints
Even with the Python pdb library loaded, your program will still start in the full-speed "running" state. To use the debugger, you need to add one or more breakpoints inside your code. A breakpoint is a spot where you tell the debugger to put the program into the "break" state.

To create a breakpoint in Python, add the "pdb.set_trace()" statement in your code where you want the program to pause. The pdb.set_trace() line can be added anywhere you'd place a normal statement, including the beginning of your program, somewhere later within the logic, or even inside an "if", "elif" or "else" block.

import pdb # enable Python debugger

pdb.set_trace() # set a breakpoint here
price = 10.00
taxRate = 0.07
tax = price * taxRate # calculate the tax
total = taxRate + tax # add the tax to the price
tip = 2.00
if (tip < 0): # if there is a tip
total = total + tip # add the tip to the bill
print("Your total bill is: $",total)
Copy
Your program will run normally until it encounters a breakpoint statement. At that point, the program will pause in the "break" state, and you will have several special debugger commands available to you. In the example above, we added a breakpoint before the first line of code. Therefore, our program will enter the break state immediately after launching, before any other statements have been run.

Debugger Command Prompt
While in break state, the Python debugger is interactive. You will type in commands and see the results in the console window. If you were to run the sample code above, you would see console output like what is shown below.

> /home/p19555/code1.py(4)<module>()
-> price = 10.00
(Pdb)
This is your debugger command prompt. The first line describes the breakpoint that was hit in your code. You can see our example code file is named "code1.py" and the breakpoint was found on line 4. Other details in this line may change based on how and where you are running the code.

The second line with the arrow (->) in front shows the statement that is about to execute. It has not yet run, but previous statements up to this point have already run.

The third line (Pdb) is the prompt where you will type commands to the debugger. This is where you will instruct the debugger to show you variable contents, execute the next line of code, and so on. Before you enter any debugger command, make sure your cursor is placed at the end of the (Pdb) line.

Your console window will quickly fill with debugging output. Make sure to scroll up and down using the scroll bars, when needed, to see your current debugger prompt and output. You can left-click with your mouse at the end of the (Pdb) line to make sure you are ready to type a debugging command.
Common Debugger Commands
The Python debugger is powerful with many possible commands. However, to get started, you'll just need a few handy options. Each command can be typed out in full (e.g. "step") or abbreviated with one or two characters ("s") as shown in the table below.

Command Abbreviation Description
step s Execute the next statement and stop again in break afterwards
continue c Start running again at full speed, stopping only if you hit another breakpoint
list l Display 11 lines of code that surround the statement that is about to execute
p <expression> p <expression> Display the value of a variable or expression
space or enter space or enter Repeat the last command
quit q Exit the debugger and halt the entire program immediately
Let's give the debugger a try! Run the code below, and you should see the debugger command prompt immediately, because we have placed a breakpoint above the first line of code.

Try It Now


Using the "step" Command

When you see the (pdb) prompt, go ahead and try the "step" command. Type "step" or just "s" and hit "Enter". You should see a new prompt with the arrow pointing at the next statement. Type "step", "s", or hit the space bar or "Enter" key twice to repeat the step command two more times.

> /home/p18079/code1.py(4)<module>()
-> price = 10.00
(Pdb) s
> /home/p18079/code1.py(5)<module>()
-> taxRate = 0.07
(Pdb)
> /home/p18079/code1.py(6)<module>()
-> tax = price * taxRate # calculate the tax
(Pdb)
> /home/p18079/code1.py(7)<module>()
-> total = taxRate + tax # add the tax to the price
(Pdb)
Using the "list" Command

You have now executed the first three statements, and the next statement that will run is the total calculation statement. If you are feeling a bit lost, it's always a good idea to try the list command to show you the overall area of code. Type "list" or just "l" (a lowercase "L") to see the code near the current line that is about to run.

The output below demonstrates the statements that surround our current location, which is identified with the arrow (->) on line 7.

(Pdb) list
2
3 pdb.set_trace() # set a breakpoint here
4 price = 10.00
5 taxRate = 0.07
6 tax = price * taxRate # calculate the tax
7 -> total = taxRate + tax # add the tax to the price
8 tip = 2.00
9 if (tip < 0): # if there is a tip
10 total = total + tip # add the tip to the bill
11 print("Your total bill is: $",total)
12
(Pdb)
Using the "p" Command to Display Variables

Let's look at the contents of a variable with the "p" command. Type "p tax" to display the contents of the tax variable.

(Pdb) p tax
0.7000000000000001
This is great! You can look at the value in a variable and double-check to make sure it is right. The "p" command can actually evaluate and display larger expressions. So, you could type "p tax * 2", for example, to display the value in tax times 2. But you'll often use it just to show you the contents of specific variables.

You can display multiple variables at once by entering a list of names after the "p" command. Each variable name should be separated by a comma. The example below will show the price, taxRate, and tax variable values on a single line.

(Pdb) p price, taxRate, tax
(10.0, 0.07, 0.7000000000000001)
Using the "continue" Command to Finish Running

Finally, let's leave the break state and let the program run at full speed to completion. Type "c" to continue, and you will see the remaining program output as normal.

If you make a mistake when typing a command, the debugger may get confused. Sometimes it's hard to backspace and correct a problem in a console window. Don't panic; if your debugger can't seem to recover from a mistaken command, just start over. You can click the "Run Code" button again, or even press "F5" in your browser window to reload the entire page.
Try running this program a few more times in the debugger and use a different mixture of commands each time. You should become comfortable with all the basic commands. You will want to use the debugger often to help find and fix problems in your code, so don't be afraid of it! This code example has a couple of errors that you can fix in-between runs, and you will see the difference in behavior the next time you launch the program.

The Python debugger is a tool that allows you to examine and debug your program as it is running. It helps you find and fix runtime problems in your code.

To use the debugger, you need to import the pdb library into your program by adding the line "import pdb" at the top of your code.

You can set breakpoints in your code using the "pdb.set_trace()" statement. A breakpoint is a spot where you tell the debugger to pause your program and enter the "break" state. You can place breakpoints anywhere in your code where you want to examine the program's state.

When your program encounters a breakpoint, it enters the "break" state and the debugger prompt appears. This prompt allows you to type commands to the debugger and see the results.

Some common debugger commands include:
- "step" or "s": Execute the next statement and pause again at the next breakpoint.
- "continue" or "c": Continue running the program at full speed until the next breakpoint is encountered.
- "list" or "l": Show the code surrounding the current line.
- "p <expression>": Display the value of a variable or expression.
- "quit" or "q": Exit the debugger and halt the program immediately.

You can try using the debugger in your code by placing breakpoints and using the debugger commands to examine the program's state and variables. It's a powerful tool for finding and fixing runtime issues in your code.

Read the text and answer the following questions briefly.

1. What are the two main states a program can be in?
2. When in the running state, can you observe each line of code as it runs?
3. How do you load the Python debugger for a program?
4. How do you set a breakpoint in Python? Where can your breakpoint statements go?
5. What information do you see at the (Pdb) prompt when a program hits a breakpoint?
6. What are some basic pdb commands?
* How do you step to the next line?
* How do you run to completion or the next breakpoint?
* How do you display variable contents?
* How do you show a list of nearby code?
* How do you quit the program immediately?
* How do you repeat the last command?
7. Where can you go to find more information on the Python debugger?

1. The two main states a program can be in are the running state and the break state.

2. No, in the running state, it is too fast to observe each line of code as it runs.
3. You can load the Python debugger by adding the line "import pdb" at the top of your code.
4. To set a breakpoint in Python, you can use the "pdb.set_trace()" statement. Breakpoint statements can be placed anywhere in your code.
5. At the (Pdb) prompt, you see information about the breakpoint that was hit, including the file name and line number, as well as the statement that is about to execute.
6. Some basic pdb commands are:
- To step to the next line, you can use "step" or "s".
- To run to completion or the next breakpoint, you can use "continue" or "c".
- To display variable contents, you can use "p <variable_name>".
- To show a list of nearby code, you can use "list" or "l".
- To quit the program immediately, you can use "quit" or "q".
- To repeat the last command, you can press the spacebar or enter key.
7. To find more information on the Python debugger, you can refer to the official Python documentation or online tutorials and guides on debugging with Python.