Debugging in Google Colab
Last Updated :
27 Nov, 2023
Having a debugger is a must while dealing with some complex or large problems that require a lot of lines of code and complexity associated with it. Although most renowned IDEs like VS Code, PyCharm, and IntelliJ have their Debuggers preinstalled with them, those who use third-party applications online like Google Colab, find it difficult to debug their code. print() statement can't always come in handy in the case of large and complex programs like Machine Learning or Deep Learning tasks. In this article, we will see how we can easily debug our code in Google Colab by using a library specially created for this purpose.
Required Modules
We will use a module named ipdb to debug our code in Google Colab, it doesn't come pre-installed so we need to install it via pip. If the user wants they can try to import the module to check if it is pre-installed or not, they will get a similar output as below -

Installing the Module
Type the following command in the Code section and run that particular cell.
!pip install ipdb

Now re-run the previous cell to check if it has successfully installed or not. If there is no error, then it has been installed successfully.
Enabling Automatic Debugger Calling
IPDB offers a great feature in which the user can just on the automatic debugger, so that if there is any error in the code, after running that code cell the debugger will automatically become active and respond to that error, even if we haven't called it nor used it anywhere inside the code. To do that write the below command and run that cell
%pdb on

Now we will deliberately write a code with an error in it and see how pdb reacts, we will also see an instance where pdb is not on and see the difference between the outputs
Python3
# Code to Demonstrate PDB
num = 25
div = num%0 # The line which has error
print(div)
We will be using the above code as a template for both pdb on and pdb off purpose.
Output when PDB is ON
When PDB is on, we will get the following output of the above code.

As we can see, apart from the Error there is a different section with ipdb> notation which is showing where the error is and waiting for the suitable command to debug it, the code will not stop executing until we write q and press Enter to stop the debugger. There are a list of commands supported by PDB, we will see the list later.
Output when PDB is off
When the PDB is off and we are using a normal cell, the output of the same code will be different, there will be no extra section to debug it, just the error and the code will stop executing immediately.

Here There is no extra section related to the Debugger, nor the code is executing even after finding an error.
How to stop Automatic Debugging?
After executing %pdb on, for the entire session that pdb will work for all the cells, to stop this the user need to type and execute the following in a cell.
%pdb off

Adding a Breakpoint
Adding a Breakpoint in between a large and complex code is a common practice, developers put breakpoint in some strategic points of the code where they think an error might occur. Adding a breakpoint will cause the code to stop executing at that certain point. Adding a breakpoint helps the developer understanding why some of the test cases are running and some are not as the execution will stop prematurely.
The module has a method named set_trace() which is used to set breakpoints in code. It generally takes no parameter to work, but if the developer wants, they can pass a parameter called context and a number, this context is used to provide some lines above or below the point which is causing the error, this is helpful while dealing with a longer and complex code as without understanding the context, developers can fix that because it may happen that after fixing that error a new error arises.
set_trace() method without context
As we can see without giving any context the method is printing only the for loop, in which the error resides.
Python3
def divide_num(n):
for i in range(n,-1,-2):
ipdb.set_trace()
print(n%i)
x = divide_num(50)
print(x)
Output

set_trace() method with context
Here we can clearly see there is a difference between the two outputs, in the above one, as context is given as 5, pdb is printing total 5 lines surrounding the one in which the error resides. This helps in understanding the surroundings of the line in which the problem is occurring and rectify it so that no other error arises.
Python3
def divide_num(n):
for i in range(n,-1,-2):
ipdb.set_trace(context=5) # providing context as 5
print(n%i)
x = divide_num(50)
x
Output

IPDB Commands
There are certain commands used in IPDB to manipulate the debugger as the developer wants. The mostly used commands and their usage is given below in a tabular format.
|
To show all the commands supported by IPDB.
|
This shows the details of the COMMAND passed after h. It bascially means "Help COMMAND"
|
This means continue the execution until there is another breakpoint
|
Go to the next line
|
Step into the next code / function
|
Continue exceuting the code until face another breakpoint or end of the Code (return statement)
|
It is the alternative of the "context" used in set_trace(), it also shows the surroundings of the code
|
Shows all the stack traces
|
List of Arguments passed while calling the function (if there is a function in the code)
|
Stop the Debugger and the execution of that cell
|
There are so many other commands that are available in IPDB, the above table consists of only the commands that are mostly used. The below picture shows all the list of commands available

Similar Reads
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Steady State Response In this article, we are going to discuss the steady-state response. We will see what is steady state response in Time domain analysis. We will then discuss some of the standard test signals used in finding the response of a response. We also discuss the first-order response for different signals. We
9 min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read
Polymorphism in Java Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca
7 min read
3-Phase Inverter An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power
13 min read
What is Vacuum Circuit Breaker? A vacuum circuit breaker is a type of breaker that utilizes a vacuum as the medium to extinguish electrical arcs. Within this circuit breaker, there is a vacuum interrupter that houses the stationary and mobile contacts in a permanently sealed enclosure. When the contacts are separated in a high vac
13 min read
AVL Tree Data Structure An AVL tree defined as a self-balancing Binary Search Tree (BST) where the difference between heights of left and right subtrees for any node cannot be more than one. Example of an AVL Tree:The balance factors for different nodes are : 12 :1, 8:1, 18:1, 5:1, 11:0, 17:0 and 4:0. Since all differences
4 min read
CTE in SQL In SQL, a Common Table Expression (CTE) is an essential tool for simplifying complex queries and making them more readable. By defining temporary result sets that can be referenced multiple times, a CTE in SQL allows developers to break down complicated logic into manageable parts. CTEs help with hi
6 min read