Python Debugger – Python pdb

Filed Under: Python
Python Pdb Module

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.

Python Debugger – Python PDB

The pdb module in Python provides us with immense features for effective debugging of python code. This includes:

  • Pause a program
  • Look at the values of variables
  • Look at program execution on every line

These are just some points that make pdb module a boon for Python programmers. Let’s look at how we can start using this module.

Starting Python Debugger

Well, it isn’t about the program. We can start pdb with any program. Here is a sample program:


class User:

    def __init__(self, runner):
        self.counter = runner

    def showSomething(self):
        for i in range(self.counter):
            print(i)
        return

user = User(5)
user.showSomething()

Let’s see the output for this program:
python pdb line execution example

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:


import pdb

class User:

    def __init__(self, runner):
        self.counter = runner

    def showSomething(self):
        for i in range(self.counter):
            pdb.set_trace()
            print(i)
        return

user = User(5)
user.showSomething()

Let’s see the output for this program:
python pdb trace, python debugger set_trace function
Press n and Enter to go to next line and watch the execution.

Post-mortem debugging

Once the program is finished with the execution process, debugging a failure in it is termed as post-mortem debugging. The module supports this as well with few more functions:


import pdb

class User:

    def __init__(self, runner):
        self.counter = runner

    def showSomething(self):
        for i in range(self.runner):
            pdb.set_trace()
            print(i)
        return

user = User(5)
user.showSomething()

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

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:


import pdb

def recursive_calls(i = 5, output = 'somethign here'):
    if i > 0:
        recursive_calls(i - 1)
    else:
        pdb.set_trace()
        print(output)
    return

if __name__ == '__main__':
    recursive_calls()

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

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

python pdb stack variable print

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:


import pdb

def recursive_calls(i = 5, output = 'somethign here'):
    if i > 0:
        recursive_calls(i - 1)
    else:
        print(output)
    return

if __name__ == '__main__':
    recursive_calls()

Nothing unusual here actually. Let’s see the demonstration with this program:
python pdb breakpoint
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:
python pdb disable breakpoint

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.

Reference: API Doc

Comments

  1. Peter says:

    Really a very good introduction to pdb. Such a helpful website having great information’s. Loved it!!

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages