How to Debug in Python

Smart debugging with the interactive python debugger, ipdb

Jake Manger
Geek Culture

--

Photo by Christina Morillo on StockSnap

Many times when working with python, you’ll hit a wall and struggle to figure out why a function isn’t working. A common basic approach to debug is to use print statements, but this can be tedious and time consuming. Fortunately, there is a better way to debug your python code: the interactive python debugger, ipdb.

In this article, we’ll discuss the basics of ipdb and how to use it to debug your python code.

What is ipdb?

ipdb is an interactive python debugger that allows you to step through your code line by line. It allows you to inspect variables and their values, set breakpoints, and even execute code on the fly. It’s a powerful tool for debugging python code and can save you a lot of time and frustration.

How to use ipdb

I’ll explain how to use ipdb using an example you might come across in a data science application. Imagine you are trying to transform your data with a log function, but are encountering a case where an error is raised:

import math


def log_transform(x):
return math.log(x)


log_transform(5)
log_transform(0)

Here, you will encounter a math domain error like so:

--

--

Jake Manger
Geek Culture

I like to write how to guides on building cool things with programming and ai. I’m also an indie developer and neuroscience/ai PhD student.