Debugging In Python: Ipdb, Getting Started.

Published on June 21, 2023 | Tags: Python debugging

Python has a debugger shipped with the standard library: pdb. It allows you to stop the execution of your program and inspect the variables, methods and current state of your python program. However, while it works and does its job, I find myself often needing a bit more help than the debugger itself.


This is where ipdb comes in. It is what iPython is to the standard interpreter. It does not add any extra functionality to the debugger itself. Instead, adds colours and autocompletion. I find this very valuable as sometimes when you are debugging something you do not know exactly what do you have, or the name of the methods, or any context.


The breakpoint


In order to start using the python debugger, you can use either of these methods:


import pdb; pdb.set_trace()

or, after python 3.7


breakpoint()

This will stop the execution of your program at the place where you set up your breakpoint. This is great to debug your application and inspect variables, state, methods if it is throwing errors and raising exceptions.


Now, how do we start ipdb instead? You can do the same:


import ipdb; ipdb.set_trace()

However, we can customise the breakpoint built-in, so it uses ipdb instead:


export PYTHONBREAKPOINT="ipdb.set_trace"

Note: Take into account that if ipdb is not installed, the function call will be ignored and the program will continue to execute.


Happy debugging!


Ferran Jovell