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:

Let’s see the output for this program:

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:

Let’s see the output for this program:
pdb-line-execution-example
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:

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

pdb-line-execution-example

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:

pdb-line-execution-example

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

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:

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:
pdb-line-execution-example
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:

pdb-line-execution-example

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

By admin

Leave a Reply