Python Debugger Module – Python Tutorials

Welcome, everyone, to this lecture on the Python debugger module. When trying to figure out what errors there are in your code, you’ve probably used the print function to try to track down the error by printing out variables within your Python script.

Fortunately, Python comes with a built-in debugger tool that allows you to interactively explore variables within the Middle operation of your Python code.

Python debugger Module

You’ve probably used a variety of print statements to try to find errors in your code. A better way of doing this is by using Python’s built-in debugger module (pdb). The pdb module implements an interactive debugging environment for Python programs. It includes features to let you pause your program, look at the values of variables, and watch program execution step-by-step, so you can understand what your program actually does and find bugs in the logic.

This is a bit difficult to show since it requires creating an error on purpose, but hopefully, this simple example illustrates the power of the pdb module.
Note: Keep in mind it would be pretty unusual to use pdb in a Jupyter Notebook setting.


Here we will create an error on purpose, trying to add a list to an integer.


x = [1,3,4]
y = 2
z = 3

result = y + z
print(result)
result2 = y+x
print(result2)
python debugger module 1

Here, it looks like we get an error! Let’s implement a set_trace() using the python pdb module. This will allow us to pause the code at the point of the trace and check if anything is wrong.

import pdb

x = [1,3,4]
y = 2
z = 3

result = y + z
print(result)

# Set a trace using Python Debugger
pdb.set_trace()

result2 = y+x
print(result2)

Output will be:

5
--Return--
> <ipython-input-1-6c36e8161fda>(11)<module>()->None
-> pdb.set_trace()
(Pdb) x
[1, 3, 4]
(Pdb) result2
*** NameError: name 'result2' is not defined
(Pdb) q
---------------------------------------------------------------------------
BdbQuit                                   Traceback (most recent call last)
<ipython-input-1-6c36e8161fda> in <module>
      9 
     10 # Set a trace using Python Debugger
---> 11 pdb.set_trace()
     12 
     13 result2 = y+x

~\Anaconda3\lib\bdb.py in trace_dispatch(self, frame, event, arg)
     90             return self.dispatch_call(frame, arg)
     91         if event == 'return':
---> 92             return self.dispatch_return(frame, arg)
     93         if event == 'exception':
     94             return self.dispatch_exception(frame, arg)

~\Anaconda3\lib\bdb.py in dispatch_return(self, frame, arg)
    152             finally:
    153                 self.frame_returning = None
--> 154             if self.quitting: raise BdbQuit
    155             # The user issued a 'next' or 'until' command.
    156             if self.stopframe is frame and self.stoplineno != -1:

BdbQuit:
python debugger module


Great! Now we could check what the various variables were and check for errors. You can use ‘q’ to quit the debugger. For more information on general debugging techniques and more methods, you can check out the official documentation of the Python debugger module: https://docs.python.org/3/library/pdb.html.


Must Read related article:

Leave a Reply

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