Debugging Python code using breakpoint() and pdb
Last Updated :
15 Jun, 2023
While developing an application or exploring some features of a language, one might need to debug the code anytime. Therefore, having an idea of debugging the code is quite necessary. Let's see some basics of debugging using the built-in breakpoint() function and pdb module.
Python breakpoint()
We know that a debugger plays an important role when we want to find a bug in a particular line of code. Here, Python comes with the latest built-in function breakpoint() which does the same thing as pdb.set_trace() in Python 3.6 and below versions. Debugger finds the bug in the code line by line where we add the breakpoint, if a bug is found then the program stops temporarily then you can remove the error and start to execute the code again.
Syntax in Python 3.7
breakpoint()
Syntax in Python 3.6 and below
import pdb; pdb.set_trace()
Debugging Python code using breakpoint() and pdb Methods
Debugging in Python using breakpoint() and pdb module requires a set of commands that needs to be followed while debugging Python code. These commands are as follows:
c: continue execution
q: quit the debugger/execution
n: step to next line within the same function
s: step to next line in this function or a called function
Here are the examples by which we can debug Python code using breakpoint() and pdb.
Debugging code using the breakpoint() function in Python
In this method, we simply introduce the breakpoint where we have doubts or somewhere we want to check for bugs or errors. We created a function to divide two numbers and added a breakpoint() function just after the function declaration. When we execute the code, it will work until a breakpoint() is found. Then we type the 'c' command which indicates 'continue'.
Python3
def debugger(a, b):
# adding a breakpoint()
breakpoint()
result = a / b
return result
print(debugger(5, 0))
Output:
Debugging with breakpoint() function in Python
In order to move further with the debugging, just type the continue "c" command and press enter.
Using "c" command to continue debugging using Python breakpoint() functionDebugging code using pdb module in Python
As the same suggests, PDB means Python debugger. To use the PDB in the program we have to use one of its methods named set_trace(). Although this will result in the same as the above method, this is another way to introduce the debugger in Python version 3.6 and below.
Example 1: In this example, we are using the pdb module to debug a code with a 'division by zero' error.
Python3
def debugger(a, b):
import pdb; pdb.set_trace()
result = a / b
return result
print(debugger(5, 0))
Output:
Debugging with pdb module in Python
In order to move further with the debugging, just type the continue "c" command and press enter.
Using "c" command to continue debugging using Python pdb module
Example 2: In this example, we are using the pdb module to debug a code with a 'list index out of range' error.
Python3
# import pdb module
import pdb;
def debugger(a):
# set a trace for debugging
pdb.set_trace()
result = [a[element] for element in range(0, len(a)+5)]
return result
print(debugger([1, 2, 3]))
Output:
Debugging with pdb module in Python
In order to move further with the debugging, just type the continue "c" command and press enter.
Using "c" command to continue debugging using Python pdb module
Similar Reads
Using ipdb to Debug Python Code
Interactive Python Debugger(IPDB) is a powerful debugging tool that is built on top of the IPython shell. It allows developers to step through their code line by line, set breakpoints, and inspect variables in real-time. Unlike other debuggers, IPDB runs inside the Python interpreter, which makes it
4 min read
Execute a String of Code in Python
Sometimes, we encounter situations where a Python program needs to dynamically execute code stored as a string. We will explore different ways to execute these strings safely and efficiently.Using the exec function exec() function allows us to execute dynamically generated Python code stored in a st
2 min read
Debugging in Python with Pdb
The PDB module in Python gives us gigantic highlights for compelling debugging of Python code. This incorporates: Â Pausing of the programLooking at the execution of each line of codeChecking the values of variables This module is already installed with installing of python. So, we only need to impo
6 min read
Python | Basic Program Crash Debugging
If the program is crashing with an exception, running the program as python3 -i can be a useful tool for simply looking around. The -i option starts an interactive shell as soon as a program terminates. From there, the environment can be explored. Code #1: Given the code Python3 1== # abc.py def fun
2 min read
Using C codes in Python | Set 1
Prerequisite: How to Call a C function in Python Let's discuss the problem of accessing C code from Python. As it is very evident that many of Pythonâs built-in libraries are written in C. So, to access C is a very important part of making Python talk to existing libraries. There is an extensive C p
4 min read
Debugging with ice cream in Python
Do you often use Python print() to debug your code? However, if you want to make a significant improvement, you could use IceCream which would make debugging faster, cleaner, and easier to read. ic(), which is short for IceCream prints both expressions/variable names and their values. ic() is faster
7 min read
Python | Add Logging to Python Libraries
In this article, we will learn how to add a logging capability to a library, but donât want it to interfere with programs that donât use logging. For libraries that want to perform logging, create a dedicated logger object, and initially configure it as shown in the code below - Code #1 : Python3 #
2 min read
How to Break a Function in Python?
In Python, breaking a function allows us to exit from loops within the function. With the help of the return statement and the break keyword, we can control the flow of the loop.Using return keywordThis statement immediately terminates a function and optionally returns a value. Once return is execut
2 min read
Loops and Control Statements (continue, break and pass) in Python
Python supports two types of loops: for loops and while loops. Alongside these loops, Python provides control statements like continue, break, and pass to manage the flow of the loops efficiently. This article will explore these concepts in detail.Table of Contentfor Loopswhile LoopsControl Statemen
2 min read
Object oriented testing in Python
Prerequisite: Object-Oriented Testing Automated Object-Oriented Testing can be performed in Python using Pytest testing tool. In this article, we perform object-oriented testing by executing test cases for classes. We write a program that has one parent class called Product and three child classes -
5 min read