How to Use PDB, the Python Debugger

“A debugger, in its simplest form, is a tool that enables users to load the program at a specific time and analyze the attributes, call stack, and whatever else users want to observe, create implicit configurations, and proceed through the original program line by line.  If the users are using Python, one can execute the code that is compiled in the command prompt, go over the code while debugging, and sometimes even enhance the efficacy by altering the values of the parameters.

Python’s PDB package makes debugging easier. It is an inbuilt debugger associated with the Python standard library. It is explicitly noted as the class PDB that utilizes the cmd (support for line-oriented command processors) and bdb (basic debugger operations) packages effectively. Whenever we don’t have access to a Graphical user interface-based debugger, the main benefit of using the PDB is that it only executes just at the command line and can also be used for debugging the programs on cloud computers.

Creating breakpoints, stepping over the script, presenting the configuration files, as well as observing stack traces are all features that PDB provides.

We only need to enter the integrated PDB and set_trace() statements to begin debugging the code. Execute the program normally, and the breakpoint we specified may cause execution to end. Therefore, it is extremely difficult to set a breakpoint on the block before the execution of the set trace () function. Breakpoint(), an inbuilt method in Python 3.7 and subsequent versions, performs similar functionality.

This post will go over how to utilize the Python debugger or PDB.”

Example no 1

In this example, we’ll add two numbers together. The code appends the strings returned by the input() function rather than adding the entered values.

import pdb

def addition(x, y):

ans = x + y

return ans


l = input("Please enter the 1<sup>st</sup> value : ")

m = input("Please enter the 2<sup>nd</sup> value : ")

s = addition(l, m)


We will incorporate the PDB header file at the commencement of the code. The inbuilt debugger for Python is known as PDB. It provides all the debugger capabilities that we need, but when we want to spruce it up a bit, we may use ipdb to add utilities from IPython to the debugger. Then we are going to define the addition() method. We provide two different variables as its parameters. The following step will include declaring a variable called “ans.”

Here we add the values of those variables, which we pass as arguments of the addition() function. This method returns the answer. Let’s call the set_trace() method. This function is associated with the PDB library. We utilize the input() method twice; the first one is used to print the line on the screen “Please enter the 1st value”. Therefore, when the user sees this message on the screen, they will enter the first value. A variable named “l” can be used to store the value.

Similarly, the second input() method displays the text “Please enter the 2nd value”. The variable “m” holds this value. It is now called the addition() function. This function contains two parameters. In the end, we apply the print() method to display the resultant value.

The relative path to the program, the line where the break statement is situated, and the package are all given in the result. In general, it indicates that the module type of the system has attained a breakpoint. If the break statement is added inside the script, its value may take place within <>. The block of the code where the processing is interrupted is displayed in the output.

Example no 2

The source code is imported by the expression, which then interrupts execution at the first block of the program. Post-mortem debugging requires starting the program’s implementation in kernel mode after the error, as it has already taken place. The utilities in PDB provide post-mortem debugging. Certain applications search for dynamic trace back and activate the debugger at the call stack segment where the error appeared. Whenever an error is detected by the application, we may see a PDB display in the outcome of the provided instance.

def multiply(i, j):

ans = i * j

return ans

u = input("Please enter the 1st value : ")

v = input("Please enter the 2nd value : ")

res = multiply(u, v)


First of all, the multiply() method is being defined. We have given two distinct variables as our arguments. We have been initializing a variable called “ans” in the following step. Here, we multiply the values of the variables that we provide to the multiply() method as arguments. This approach returns the result.

Now, we would use the input() function two times, the first time presenting the statement “Please enter the 1st value” on the screen. Therefore, when the user sees this text on the screen, they will provide the first value. A variable named “u” can be used to store the value. The second input() function similarly shows a message “Please enter the 2nd value.” The second integer must be taken as input. The variable “v” contains this value. The multiply() method is now invoked. The values specified by the user will pass as two arguments in this method. Finally, we will be using the print() function to show the outcome.


In this article, we have talked about how to use the python debugger “PDB”. Debugging is a term frequently used in the process of software development to define the framework for identifying and fixing programmatic errors. The standard library for Python includes the PDB package, a collection of tools for debugging the code. A PDB class contains the definition of debugging capabilities. The bdb and cmd packages are being used by the module implicitly. We execute two examples, and in the first one, we utilize the PDB debugger to get rid of the exception. And in the second instance, we wouldn’t have used “PDB,” thus we get an error.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content