The Wayback Machine - https://web.archive.org/web/20240614141550/https://www.geeksforgeeks.org/using-ipdb-to-debug-python-code/
Open In App

Using ipdb to Debug Python Code

Last Updated : 24 Aug, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Internet Pinball Machine Database(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 easier to use and integrate with other tools.

Required Modules

pip install ipdb

How to use IPDB

After the debugger is installed, now you can start using IPDB by adding the following line of code:

import ipdb; 
#code
ipdb.set_trace()

This line of code will set a breakpoint in your code at the point where it is executed. When the code reaches this point, the debugger will pause execution and drop you into an interactive shell where you can start debugging.

From here, you can use various IPDB commands to step through your code, inspect variables, and more. Some of the most commonly used commands include:

command

keyword

function

next

n

Execute the current line and move to the next one

step

s

Step into the function call on the current line

continue

c

Continue execution until the next breakpoint or the end of the program

print

p

Print the value of a variable

quit

q

Quit the debugger

Examples

Iterating through the loop

The function factorial(n), which is defined in this code, uses recursion to get the factorial of a given integer n. In this case, the method returns 1 if n is less than or equal to 0. If not, it calls itself recursively with n-1 as the argument and multiplies the outcome by n. After the factorial() function declaration, the code additionally places a breakpoint using ipdb.set_trace().

Python3




import ipdb
 
def factorial(n):
    if n <= 0:
        return 1
    else:
        return n * factorial(n-1)
 
ipdb.set_trace()
 
num = 5
fact = factorial(num)
 
print("Factorial of", num, "is", fact)


Output :

IPDB example

the terminal will open like this and you can control the compiler using commands

IPDB example

giving ‘n’ command, execution will be taken to the next breakpoint

IPDB example

‘p’ command will print the value of the variable i.e. ‘num’

IPDB example

‘s’ step into the function call on the current line

IPDB example

‘c’ takes to the next breakpoint or end of the program

Viewing sorting program

Sort_numbers() and main() are two functions defined in this code. sort_numbers() takes a list of numbers as input, uses ipdb.set_trace() to set a breakpoint, uses the sorted() function to sort the numbers in descending order, and then returns the sorted list.

Python3




import ipdb
 
def sort_numbers(numbers):
    ipdb.set_trace()
    sorted_numbers = sorted(numbers, reverse=True)
    return sorted_numbers
 
 
def main():
    numbers = [5, 2, 8, 1, 9, 4]
    sorted_numbers = sort_numbers(numbers)
    print(sorted_numbers)
 
main()


Output:

When we run the code again, we’ll see that it pauses at the breakpoint, and we can use various IPDB commands to examine the state of the program. For example, we can use the print command to check the value of the numbers argument :

IPDB example

IPDB example

Simple multiply and add the program

In this code, we are using the ipdb.set_trace() instruction, the code places a breakpoint. When this happens, the Python debugger will halt the program’s execution at that particular moment to allow the user to investigate the status of the program at that time. Following the breakpoint, the program executes add(), passing result1 and y as inputs, and saving the outcome in result2. The value of result2 is finally written to the console.

Python3




import ipdb
 
def multiply(x, y):
    result = x * y
    return result
 
def add(x, y):
    result = x + y
    return result
 
def main():
    x = 5
    y = 10
    result1 = multiply(x, y)
    ipdb.set_trace()   #breakpoint created by ipdb
    result2 = add(result1, y)
    print(result2)
 
main()


Output :

IPDB example

IPDB example

Difference between pdb debugger and ipdb debugger

Both PDB and IPDB support column-based debugging, which means that you can set a breakpoint at a specific column in a line of code. However, IPDB offers some additional functionality that makes it more user-friendly and powerful than PDB.

features  

    pdb   

  ipdb     

User Interface

  1. No syntax highlighting  
  2. No tab-completion.
  3. Don’t allow customization

 

  1. Syntax highlighting
  2. Tab-completion.
  3. Allows you to customize the way that your debugger display looks

 

Additional Features

Limited additional features beyond basic debugging capabilities.

Additional features such as post-mortem debugging and the ability to set breakpoints at specific columns.

Installation

Included Python by default.

Needs to be installed separately using pip.

Compatibility                    

Compatible with IPDB.

Fully compatible with PDB



Similar Reads

Python | Generate QR Code using pyqrcode module
Let's see how to generate QR code in Python using pyqrcode module. pyqrcode module is a QR code generator. The module automates most of the building process for creating QR codes. This module attempts to follow the QR code standard as closely as possible. The terminology and the encodings used in pyqrcode come directly from the standard. Installati
2 min read
Debugging Python code using breakpoint() and pdb
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 w
3 min read
Issues with using C code in Python | Set 1
Prerequisite: Using C codes in Python | Set 1, Set 2 Issue #1 : If using ctypes then there is a problem that the original C code may use language that don’t map cleanly to Python. Let's take the example of divide() function as it returns a value through one of its arguments. It is a common C technique but it is often not clear how it is supposed to
2 min read
Issues with using C code in Python | Set 2
Prerequisite: Issues with using C code in Python | Set 1 The DoubleArrayType class can handle the situation of Python having different forms like array, numpy array, list, tuple. In this class, a single method from_param() is defined. This method takes a single parameter and narrow it down to a compatible ctypes object (a pointer to a ctypes.c_doub
3 min read
Python code formatting using Black
Writing well-formatted code is very important, small programs are easy to understand but as programs get complex they get harder and harder to understand. At some point, you can’t even understand the code written by you. To avoid this, it is needed to write code in a readable format. Here Black comes into play, Black ensures code quality. What is B
3 min read
Convert Python Code to a Software to Install on Windows Using Inno Setup Compiler
Users are only given the installer for any kind of software, companies didn't give the source code file to the user. Although some open source software is available with its source code. We can also make an installer from our python source code so that you don't have to share your source code.Write your python code using any graphical library, i am
2 min read
Python - Morse Code Translator GUI using Tkinter
Prerequisites : Introduction to tkinter | Morse Code Translator Python offers multiple options for developing a GUI (Graphical User Interface). Out of all the GUI methods, Tkinter is the most commonly used method. Python with Tkinter outputs the fastest and easiest way to create GUI applications. In this article, we will learn how to create a Morse
6 min read
Application to get address details from zip code Using Python
Prerequisite: Tkinter In this article, we are going to write scripts to get address details from a given Zip code or Pincode using tkinter and geopy module in python, Tkinter is the most commonly used GUI module in python to illustrate graphical objects and geopy is a python module to locate the coordinates of addresses, cities, countries, landmark
2 min read
Wi-Fi QR Code Generator Using Python
Prerequisite: Getting Saved Wifi Passwords using Python We know the wireless network is the most common network adapter for today, Because of its supports portability and User friendly. In this article, we will see how we can get the current saved Wi-Fi name and passwords and generate QR code to connect other devices using Python. Before starting w
2 min read
Get Bank details from IFSC Code Using Python
The Indian Financial System Code (IFSC) is an 11-digit alpha-numeric code used to uniquely classify bank branches within the National Electronic Fund Transfer (NEFT) network by the Central Bank. In this article, we are going to write python scripts to get details of the Bank from the given IFSC code. Methods 1: Module Used: ifscapi: The IfscApi Mod
2 min read
Article Tags :
Practice Tags :