Python : Debugging using Python’s built-in debugger module (pdb)

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.

Run below example code and observer that program stops at break point where “pdb.set_trace()” is writter, you can watch value of all variable like x,y,z after that point.


import pdb

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

result = y + z
print result

# Set a trace using Python Debugger

result2 = y+x
print result2

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, check out the official documentation:

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.