Please enable JavaScript to use CodeHS

Using the Python Debugger

Learn how to use the pause-step Python Debugger on CodeHS

By Calvin Studebaker

Just an Analog Guy living in a Digital World

Debuggers are very useful tools for analyzing the execution of your program. Debuggers allow you to pause your program while it is running, and slowly step through the code line by line to see the flow of execution. You can also print out the values of each variable at each step of your program.


The CodeHS IDE allows you to pause and step through your Python programs by entering "Debug Mode" in your editor settings:

Let's try out the debugger on a real program! Press "Run" to start the debugger, and "Step" to start stepping through the code

Debugger Commands

The debugger provides a few options for stepping through the code:

  1. Step - steps to the very next instruction, regardless of which function the instruction is located in
  2. Next - skips to the next instruction in the current function. Next will not step into any function calls, it will always go to the next command within the current function.
  3. Continue - continues to run the program without pausing


Note

When debugging Python programs, we generally want to use Next anytime we are on an import statement, or about to call a Python library function that we didn't write ourselves.

In general, you'll only want to use Step to step into a function call for a function you personally wrote.


Your Turn

Try using Step and Next in this program!

  1. Start off with a Next command to skip over the import statement
  2. From here try using only Step to finish the program. It should take 19 Steps to finish every instruction.
  3. Now try again, this time using only Next to finish the program. It should only take 6 Nexts to finish, since Next does not step into any function calls!

Printing Variables

While debugging your program, you can use the print function to print out the values stored inside variables in your code:

Your Turn

Step to line 10 of this program, then:

  • Type print(name) into the debugger console to see the value of the name variable
  • Type print(phonebook) into the debugger console to see the value of the phonebook dictionary

Now step to line 14 of this program, then:

  • Type print(i) to see the value of the for loop counter

Breakpoints

What if you want to pause your program on a specific line of code? This is where breakpoints come in handy.


While using the debugger, you can click on any line number to set a breakpoint at that line. When you press Run or Continue, the debugger will pause once it hits that line of code.

Your Turn

Try setting some breakpoints in the following program!


Click on a line number to set a breakpoint at that line.

Press Run to start the program and pause at the first breakpoint you set.

Press Continue to continue to the next breakpoint.


Note

Breakpoints must be put in place before you press Run.

If you modify your breakpoints while your program is running, you'll need to re-run your program in order to pause at your new breakpoints.

Curious to Learn More?

CodeHS is using the industry standard built-in Python debugger, PDB, to pause and step through your Python programs.


In this tutorial, you learned about using the following PDB commands:

  • step - steps to the next instruction
  • next - skips to the next instruction in the current method
  • cont - continues execution without pausing
  • print - prints out the value of a variable
  • break - set a breakpoint at a specific line of code


These are the main commands you'll be using to debug Python programs, but you can check out all the PDB commands at your disposal by reading the PDB Documentation