Python Debugger – Python pdb

Python pdb module provides an interactive debugging environment for Developers to debug Python programs. Today we will explore python debugger examples using pdb module and look at the various ways of debugging a python program.

We can go to next line in pdb execution by pressing ‘n’ followed by Enter key.

Starting pdb within the program

Above example started pdb from the command line, so, pdb tracing started from the very first line of execution. Usually, we only want to debug a specific area of a program which comes much after the program has started.

To achieve this, we use pdb in our programs itself. Let’s look at a code snippet:

Here, we have mentioned self.runner which doesn’t exist. We can try these functions inside Python interpreter itself:

Checking variables on the Stack

One of the points of using a debugger at all is that we should be able to check the program stack and what variables are stored on that stack during program execution. We can do this with pdb as well. Let’s look at a code snippet:

This is a simple example for a recursion. Let’s see the output for this program:

Note that we used the where command to print variables on stack. We can also print a specific variable as:

For variables which might contain large values like file data etc., we also pretty print the values. So, if the variables is data, we can run the command:

pp data

Python pdb Breakpoint

Using n to go to next line is useful but what if you actually know where to stop? Also, debugging the whole program and moving through each line is tedious when you know where you want to actually stop!

With pdb, we can also use breakpoints. For this, we need to inform pdb which line to set a breakpoint to. Here is a sample program and demonstration:

Nothing unusual here actually. Let’s see the demonstration with this program:
We used the break keyword along with the line number where we want to set the breakpoint.

Managing Breakpoints

As we saw in the last demonstration, when we applied a breakpoint to a line, it was assigned an identifier as Breakpoint 1. The numeric part can be used as an ID to enable and disable these breakpoints interactively. Disabling a breakpoint with disable keyword informs the debugger not to halt execution when that line is reached. The breakpoint is still stored but ignored.

Let’s see how this is done:

Conclusion

In this post on python debugging, we saw how we can effectively debug a Python program and identify any issues we face using pdb module.

Popular Categories

Newsletter for You

Don't miss out!

Subscribe To Newsletter

We promise not to spam you. Unsubscribe at any time.

Invalid email address

Thanks for subscribing!

JournalDev is one of the most popular websites for Java, Python, Android, and related technical articles. Our tutorials are regularly updated, error-free, and complete. Every month millions of developers like you visit JournalDev to read our tutorials.

JournalDev was founded by Pankaj Kumar in 2010 to share his experience and learnings with the whole world. He loves Open source technologies and writing on JournalDev has become his passion.