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

Debugging in Google Colab

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.

Similar Reads

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...

Adding a Breakpoint

...