Get to Know pdb, the Python Debugger

Data

Get to Know pdb, the Python Debugger

While my day-to-day is spent working within Curator’s PHP platform, my favorite programming language is Python. I used it daily in a previous life after learning it on the job nearly two decades ago. I’m not going to lie—when I first started learning it, I actually thought, “This language is so easy, there’s no way it can be used to do anything useful.” As you probably know, based on how many things in the world are built using it, it didn’t take long for me to eat those words … and were they ever spicy!

Behold: pdb

One of my favorite built-in features of the language that I feel not enough people know about is the Python debugger (pdb). It’s a game-changer when it comes to troubleshooting and debugging Python scripts. If you find yourself adding a bunch of logging statements, running the program until it dies a horrible, fiery death, then searching through those logs to figure out what happened like some sort of text-based autopsy, may I be so bold as to say that you’re probably doing it wrong?

Effective Usage of pdb

There are many different ways to use pdb, but the simplest is by using its pdb.set_trace() function. Wherever you put this line in your code, the Python interpreter will stop and present an interactive debugger command prompt. From here, you can run any arbitrary Python code, or issue pdb commands to step through or even into the code by using next and step, respectively. Even these have shorthand abbreviations to make things quicker: n and s. When you pair this with Python’s built-in dir() function, you can inspect anything you need about the current state of the program and trace the rest of the execution until you have identified the issue.

If you have a huge loop or a lot of logic between the interesting places in the code, you can put in multiple pdb.set_trace() statements, inspect what you need and issue the continue command (or equivalent c command) to skip ahead to the next break point.

I don’t want to be accused of impersonating Captain Obvious, but I feel I should also point out that you can put these debugging break statements inside special if statements or exception handlers to only trip when very specific conditions are met. This is helpful when debugging a function that may be called countless times during the execution before failing. It can get annoying to have it break into the debugger a bunch of times before you need it. In this way, you may have 99 problems, but pdb ain’t gonna be one.

See It in Action

Let’s say you have the following Python script, and it keeps printing 99 times even though you want it to print 100 times. Use your imagination to pretend it’s as complex as something you deal with in your own day-to-day life:

Here’s a simple example of how to use the Python debugger to figure out why it doesn’t behave like you know it should:

Now, hopefully, you’re able to keep things rolling without interruption or inefficiencies.

More About the Author

Matthew Orr

Curator Engineer
Git Nested Branching with Squash Bird nests? Tree limbs? Summer squash? There sure are a lot of plant, animal and food terms in that title. What are we talking about ...
Get to Know pdb, the Python Debugger While my day-to-day is spent working within Curator’s PHP platform, my favorite programming language is Python. I used it daily in a ...

See more from this author →

Subscribe to our newsletter

  • I understand that InterWorks will use the data provided for the purpose of communication and the administration my request. InterWorks will never disclose or sell any personal data except where required to do so by law. Finally, I understand that future communications related topics and events may be sent from InterWorks, but I can opt-out at any time.
  • This field is for validation purposes and should be left unchanged.

InterWorks uses cookies to allow us to better understand how the site is used. By continuing to use this site, you consent to this policy. Review Policy OK

×

Interworks GmbH
Ratinger Straße 9
40213 Düsseldorf
Germany
Geschäftsführer: Mel Stephenson

Kontaktaufnahme: markus@interworks.eu
Telefon: +49 (0)211 5408 5301

Amtsgericht Düsseldorf HRB 79752
UstldNr: DE 313 353 072