Debugging applications can sometimes be an unwelcome activity. You’re busy working under a time crunch and you just want it to work. However, at other times, you might be learning a new language feature or experimenting with a new approach and want to understand more deeply how something is working.
Regardless of the situation, debugging code is a necessity, so it’s a good idea to be comfortable working in a debugger. In this tutorial, I’ll show you the basics of using pdb, Python’s interactive source code debugger.
I’ll walk you through a few common uses of pdb. You may want to bookmark this tutorial for quick reference later when you might really need it. pdb, and other debuggers, are indispensable tools. When you need a debugger, there’s no substitute. You really need it.
By the end of this tutorial, you’ll know how to use the debugger to see the state of any variable in your application. You’ll also be able to stop and resume your application’s flow of execution at any moment, so you can see exactly how each line of code affects its internal state.
This is great for tracking down hard-to-find bugs and allows you to fix faulty code more quickly and reliably. Sometimes, stepping through code in pdb and seeing how values change can be a real eye-opener and lead to “aha” moments, along with the occasional “face palm”.
pdb is part of Python’s standard library, so it’s always there and available for use. This can be a life saver if you need to debug code in an environment where you don’t have access to the GUI debugger you’re familiar with.
The example code in this tutorial uses Python 3.14. You can find the source code for these examples on GitHub.
At the end of this tutorial, there is a quick reference for Essential pdb Commands.
There’s also a printable pdb Command Reference you can use as a cheat sheet while debugging:
Free Bonus: Click here to get a printable “pdb Command Reference” (PDF) that you can keep on your desk and refer to while debugging.
Take the Quiz: Test your knowledge with our interactive “Python Debugging With Pdb” quiz. You’ll receive a score upon completion to help you track your learning progress:
Interactive Quiz
Python Debugging With PdbPractice using pdb, Python's built-in debugger. Check variables, step through code, set conditional breakpoints, and read a stack trace.
Getting Started: Printing a Variable’s Value
In this first example, we’ll look at using pdb in its simplest form: checking the value of a variable.
Insert the following code at the location where you want to break into the debugger:
import pdb; pdb.set_trace() When the line above is executed, Python stops and waits for you to tell it what to do next. You’ll see a (Pdb) prompt. This means that you’re now paused in the interactive debugger and can enter a command.
Starting in Python 3.7, there’s another way to enter the debugger. PEP 553 describes the built-in function breakpoint(), which makes entering the debugger easy and consistent:
breakpoint() By default, breakpoint() will import pdb and call pdb.set_trace(), as shown above. However, using breakpoint() is more flexible and allows you to control debugging behavior via its API and use of the environment variable PYTHONBREAKPOINT. For example, setting PYTHONBREAKPOINT=0 in your environment will completely disable breakpoint(), thus disabling debugging. If you’re using Python 3.7 or later, I encourage you to use breakpoint() instead of pdb.set_trace().
You can also break into the debugger, without modifying the source and using pdb.set_trace() or breakpoint(), by running Python directly from the command-line and passing the option -m pdb. If your application accepts command-line arguments, pass them as you normally would after the filename. For example:
$ python3 -m pdb app.py arg1 arg2 There are a lot of pdb commands available. At the end of this tutorial, there is a list of Essential pdb Commands. For now, let’s use the p command to print a variable’s value. Enter p variable_name at the (Pdb) prompt to print its value.
Let’s look at the example. Here’s the example1.py source:
#!/usr/bin/env python3 filename = __file__ import pdb; pdb.set_trace() print(f'path = {filename}') If you run this from your shell, you should get the following output:
Read the full article at https://realpython.com/python-debugging-pdb/ »
[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short & sweet Python Trick delivered to your inbox every couple of days. >> Click here to learn more and see examples ]
