Python debugging (pdb) - quick tip

Sometimes you want to use multiple breakpoints like so:

# some function/file...
import pdb; pdb.set_trace()
some_python_code_on_one_line()

# some other function/file...
import pdb; pdb.set_trace()
some_other_python_code_on_one_line()

I personally get lost sometimes, tracking through many files and having to read the method name and remember where exactly it is. So it can help me keep track if I leave myself a message:

# some function/file...
import pdb; pdb.set_trace()
s = 'Some function/file'
some_python_code_on_one_line()

# some other function/file...
import pdb; pdb.set_trace()
s = 'Some other function/file'
some_other_python_code_on_one_line()

In some cases (when running django on my mac, for example), I can even do this:

# some function/file...
import pdb; pdb.set_trace()
"""Some function/file"""
some_python_code_on_one_line()

... so it actually looks like a bit cleaner, like a comment.

So instead of:

> /path/to/some/file.py(##)method()
-> some_python_code_on_one_line
(Pdb)

You see:

> /path/to/some/file.py(##)some_function()
-> s = 'Some function/file'
(Pdb)

Hope that helps someone.

Longevity by Uplift

Think a long life has to come with losing your mind or your body? Think again! Age is strength. 💪 Mind & body movers for how to live a long, healthy and balanced life on your own terms.

Get a few carefully curated articles once a month. Here's a sample.