HOLIDAY SALE! Save 50% on Membership with code HOLIDAY50. Save 15% on Mentorship with code HOLIDAY15.

8) Debugging Lesson

What is Python PDB

5 min to complete · By Martin Breuss

To practice interacting with pdb, you've written a short script:

a = 0
b = 42
breakpoint()
a += b
breakpoint()

After setting a breakpoint() in your code and executing your script, you'll be launched into a debugger session with Python's default debugger, pdb. You'll be greeted by a prompt:

(venv) ➜  python debugging.py
> /Users/martin/Documents/codingnomads/debugging.py(4)<module>()
-> a += b
(Pdb)

You'll find a highlighted prompt next to (Pdb). When you see this, then you're right inside an interactive debugging session.

Explore The Debugger

As mentioned earlier, the interactive debugger session gives you the opportunity to explore the current state of your program. You can:

  • inspect the value of all variables that are currently defined
  • interact with these variables beyond just printing them

With this in mind, go ahead and explore the current state of your script.

Debugger Practice

  • What is the output of print(a) if you run it right after starting the debugger?
  • Can you also inspect the value of b?
  • What happens if you execute print(b + 3) in the debugger?
  • What is the output of print(a) at the second breakpoint()?
  • Can you change the value of one of your variables using an assignment as you would inside your script?
Colorful illustration of a light bulb

Info: you can step forward in your code execution by typing next or n and pressing Enter.

Going through these tasks shows you some of the possibilities and some limitations of the interactive debugger. But while you'll run into errors when trying to assign a new variable or change the value of an existing variable in the way you're used to, this is actually possible, and you'll learn how to do it in the next lesson. You'll also get to know the most important commands and shortcuts for interacting with pdb.

Colorful illustration of a light bulb

Info: It's possible to see the code written further up, but you can't see any previous states of your variables.

In a future lesson, you'll get to know the most important commands for interacting with pdb in an interactive debugger session.

Colorful illustration of a light bulb

Additional Resources

Summary: What is Python PDB

  • Interactive debugging is the practice of inspecting your code to uncover potential errors. It consists of the following broad steps:
    1. Declare specific points in your script where you want to inspect the current state of your program. These are called breakpoints, and you can set them with breakpoint().
    2. Execute your code as you normally would. This will start an interactive debugger session at the first breakpoint().
    3. Explore the current state of your program.
    4. Step to the next breakpoint() and continue exploring.
  • One limitation of a basic debugger is that you can only go forward. Stepping backward through your code doesn't work because the debugger doesn't keep a memory of the previous states of your program.
  • Over the years, a couple of new libraries have come out that improve on pdb's basic workflow, but since pdb comes with Python's standard library, it's still a popular debugging tool that you should know how to use.