Python Debugger – Python pdb
Last Updated :
04 Nov, 2022
Debugging in Python is facilitated by pdb module (python debugger) which comes built-in to the Python standard library. It is actually defined as the class Pdb which internally makes use of bdb(basic debugger functions) and cmd (support for line-oriented command interpreters) modules. The major advantage of pdb is it runs purely in the command line, thereby making it great for debugging code on remote servers when we don’t have the privilege of a GUI-based debugger.
pdb supports:
- Setting breakpoints
- Stepping through code
- Source code listing
- Viewing stack traces
Starting Python Debugger
There are several ways to invoke a debugger
- To start debugging within the program just insert import pdb, pdb.set_trace() commands. Run your script normally, and execution will stop where we have introduced a breakpoint. So basically we are hard coding a breakpoint on a line below where we call set_trace(). With python 3.7 and later versions, there is a built-in function called breakpoint() which works in the same manner. Refer following example on how to insert set_trace() function.
Example1: Debugging a Simple Python program of addition of numbers using Python pdb module
Intentional error: As input() returns string, the program cannot use multiplication on strings. Thus, it’ll raise ValueError.
Python3
import pdb
def addition(a, b):
answer = a * b
return answer
pdb.set_trace()
x = input("Enter first number : ")
y = input("Enter second number : ")
sum = addition(x, y)
print(sum)
|
Output :

set_trace
In the output on the first line after the angle bracket, we have the directory path of our file, line number where our breakpoint is located, and <module>. It’s basically saying that we have a breakpoint in exppdb.py on line number 10 at the module level. If you introduce the breakpoint inside the function, then its name will appear inside <>. The next line is showing the code line where our execution is stopped. That line is not executed yet. Then we have the pdb prompt. Now to navigate the code, we can use the following commands :
| Command |
Function |
| help |
To display all commands |
| where |
Display the stack trace and line number of the current line |
| next |
Execute the current line and move to the next line ignoring function calls |
| step |
Step into functions called at the current line |
Now, to check the type of variable, just write whatis and variable name. In the example given below, the output of type of x is returned as <class string>. Thus typecasting string to int in our program will resolve the error.
Example 2: Checking variable type using pdb ‘whatis’ command
We can use ‘whatis‘ keyword followed by a variable name (locally or globally defined) to find its type.
Python3
a = 20
b = 10
s = 0
for i in range(a):
s += a / b
b -= 1
|

Finding variable type using whatis command in pdb
- From the Command Line: It is the easiest way of using a debugger. You just have to run the following command in terminal
python -m pdb exppdb.py (put your file name instead of exppdb.py)
This statement loads your source code and stops execution on the first line of code.
Example 3: Navigating in pdb prompt
We can navigate in pdb prompt using n (next), u (up), d (down). To debug and navigate all throughout the Python code, we can navigate using the mentioned commands.
Python3
a = 20
b = 10
s = 0
for i in range(a):
s += a / b
b -= 1
|
Output :

Navigate in pdb prompt using commands
Example 4: Post-mortem debugging using Python pdb module
Post-mortem debugging means entering debug mode after the program is finished with the execution process (failure has already occurred). pdb supports post-mortem debugging through the pm() and post_mortem() functions. These functions look for active trace back and start the debugger at the line in the call stack where the exception occurred. In the output of the given example, you can notice pdb appear when an exception is encountered in the program.
Python3
def multiply(a, b):
answer = a * b
return answer
x = input("Enter first number : ")
y = input("Enter second number : ")
result = multiply(x, y)
print(result)
|
Output :
Checking variables on the Stack
All the variables including variables local to the function being executed in the program as well as global are maintained on the stack. We can use args(or use a) to print all the arguments of a function which is currently active. p command evaluates an expression given as an argument and prints the result.
Here, example 4 of this article is executed in debugging mode to show you how to check for variables :

checking variable values
Python pdb Breakpoints
While working with large programs, we often want to add a number of breakpoints where we know errors might occur. To do this you just have to use the break command. When you insert a breakpoint, the debugger assigns a number to it starting from 1. Use the break to display all the breakpoints in the program.
Syntax:
break filename: lineno, condition
Given below is the implementation to add breakpoints in a program used for example 4.

Adding_breakpoints
Managing Breakpoints
After adding breakpoints with the help of numbers assigned to them, we can manage the breakpoints using the enable and disable and remove command. disable tells the debugger not to stop when that breakpoint is reached, while enable turns on the disabled breakpoints.
Given below is the implementation to manage breakpoints using Example 4.

Manage_breakpoints
Similar Reads
Python | Numpy np.lagvander3d() method
np.lagvander3d() method is used to returns the Vandermonde matrix of degree deg and sample points x, y and z. Syntax : np.lagvander3d(x, y, z, deg) Parameters: x, y, z :[ array_like ] Array of points. The dtype is converted to float64 or complex128 depending on whether any of the elements are comple
2 min read
Python | sympy.Matrix.eigenvals() method
With the help of sympy.Matrix.eigenvals() method, we can find the eigen values of the matrix. Syntax : sympy.Matrix().eigenvals() Return : Return the eigen values of a matrix. Example #1 : In the given example we can see that the sympy.Matrix.eigenvals() method is used to find the eigen values of a
1 min read
Python - Import module outside directory
Modules are simply a python .py file from which we can use functions, classes, variables in another file. To use these things in another file we need to first import that module into that file. If the module exists in the same directory as the file, we can directly import it using the syntax import
4 min read
matplotlib.patches.Rectangle in Python
Matplotlib is an amazing visualization library in Python for 2D plots of arrays. Matplotlib is a multi-platform data visualization library built on NumPy arrays and designed to work with the broader SciPy stack. matplotlib.patches.Rectangle The matplotlib.patches.Rectangle class is used to rectangle
2 min read
How to create custom 404 page in CodeIgniter ?
We often see the 404 error page while using websites. This error page will encounter only when we navigate to a broken link. In CodeIgniter, it is possible to create a custom 404 page to change the look of the 404 page. So in this article, we will look into how to create the custom 404 error page. I
2 min read
jQuery UI Draggable snapMode Option
jQuery UI consists of GUI widgets, visual effects, and themes implemented using the jQuery JavaScript Library. jQuery UI is great for building UI interfaces for the webpages. It can be used to build highly interactive web applications or can be used to add widgets easily. In this article, we are goi
2 min read
wxPython | AddStretchableSpace() function in wx.ToolBar
In this particular article we are going to learn about AddStretchableSpace() function of wx.ToolBar class of wxPython. AddStretchableSpace() adds a space between Tools in toolbar. Any space not taken up by the fixed items (all items except for stretchable spaces) is distributed in equal measure betw
1 min read
Python OpenCV: Optical Flow with Lucas-Kanade method
Prerequisites: OpenCV OpenCV is a huge open-source library for computer vision, machine learning, and image processing. OpenCV supports a wide variety of programming languages like Python, C++, Java, etc. It can process images and videos to identify objects, faces, or even the handwriting of a human
3 min read
How to Install glob in Python on MacOS?
In this article, we will learn how to install glob in Python on MacOS. Glob is a general term used to define techniques to match specified patterns according to rules related to Unix shell. Linux and Unix systems and shells also support glob and also provide function glob() in system libraries. Inst
2 min read
Snowfall display using Pygame in Python
Not everybody must have witnessed Snowfall personally but wait a minute, What if you can see the snowfall right on your screen by just a few lines of creativity and Programming. Before starting the topic, it is highly recommended revising the basics of Pygame. Steps for snowfall creation 1. Importin
3 min read