How to trace your Python program with pdb

There are many useful Python packages to the Python coder out there. One of them is pdb, a package which can help to trace the state of a program in the Python computer programming language with the main purpose of debugging the code of an application or a project.

The purpose of this very short tutorial is to give the beginner a simple introduction to the builtin python debugger, through an example.

Before going any further with the article, make sure to write the following Python function in a new file, exactly like shown below.

What's the one thing every developer wants? More screens! Enhance your coding experience with an external monitor to increase screen real estate.

def find_sum(a,b):
sum = a + b
return sum

Then on the top level of your Python module which you are currently coding, include the Python default debugger by using the import statement shown below.

import pdb

So far, the module should look like the one which is shown in the following screenshot.

The default Python debugger has many functionalities. For the purpose of this tutorial, the syntax shown below is more than enough, especially to the beginners.

pdb.set_trace()

The above command, part of the builtin debugger in the Python computer programming language, set a trace in a specific part of a Python application, depending on the part of the code where one places it. For the purpose of this tutorial, we are going to place it inside the function find_sum, in our module.

Make sure to set up the Python builtin debugger like shown in the following script.

When the find_sum function will get executed the program will freeze, only a specific amount of Python code will be run by the Python interpreter.

To see things for yourself, make sure to call the function find_sum in the Python module you have created like shown below.

Then run the Python script you have coded, from the command line.

python test.py

The above command makes use of the Python interpreter and tells it to execute a Python script which in my case is called test.py. Once the command is executed on my unix based console, the following output is being displayed.

As you can see from the above screenshot, the debugger has been triggered. According to the output, the simple Python computer program has frozen on the return sum statement. The Python coder can easily debug the values of local variables inside their function; in this case they can check the value of the variable sum before the program has been executed entirely.

Type sum in the console like shown below.

sum

The following will come up.

Now type q in the console, to quit the builtin debugger.

q

Final thoughts

The Python builtin debugger is very useful when the Python coder wants to inspect their code during the execution. The purpose of this very short tutorial was to give a basic introduction on how the Python geek can trace their computer programs by using the builtin debugger offered by Python.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Author: Rishabh

Rishabh is a full stack web and mobile developer from India. Follow me on Twitter.

Leave a Reply

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