Code Yarns ‍👨‍💻
Tech BlogPersonal Blog

How to use PDB Python debugger

📅 2016-Aug-23 ⬩ ✍️ Ashwin Nanjappa ⬩ 📚 Archive

When the Python interpreter encounters an error or exceptional condition while running your code, it quits the program and prints a stack trace. I typically would like to inspect the stack at the point of error to figure out the cause of it. It would be nice to print out some of the local variables you suspect at that point, walk up the stack frames to the calling functions and inspect some of their variables too. All of this is easily possible in Python by using the Python debugger module!

$ pdb

$ python -m pdb

$ pdb3

$ python3 -m pdb

When run explicitly like this, PDB will stop at the first function.

import pdb; pdb.set_trace()
p foobar
p type(foobar)

Installing it is easy:

$ sudo apt install python-ipdb
$ sudo apt install python3-ipdb

Using it is similar to PDB:

$ ipdb

$ python -m ipdb

$ ipdb3

$ python3 -m ipdb

Tried with: Python 3.5.1 and Ubuntu 16.04