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 various way of debugging a python program.
Table of Contents
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:
Copyclass 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:
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:
Copyimport 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:
Press n and Enter to go to next line and watch the execution.
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:
Copyimport 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:
Checking variables on the Stack
One of the point 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:
Copyimport 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:
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:
Python pdb Breakpoint
Using n to go to next line is useful but what if you actually know where to stop? Also, debugging 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:
Copyimport 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:
We used the
break keyword along with the line number where we want to set the breakpoint.
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:
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