Code Yarns ‍👨‍💻
Tech BlogPersonal Blog

How to use PDB Python debugger

📅 2016-Aug-23 ⬩ ✍️ Ashwin Nanjappa ⬩ 🏷️ ipdb, pdb ⬩ 📚 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 my_script.py

$ python -m pdb my_script.py

$ pdb3 my_script.py

$ python3 -m pdb my_script.py

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

$ python -m ipdb my_script.py

$ ipdb3 my_script.py

$ python3 -m ipdb my_script.py

Tried with: Python 3.5.1 and Ubuntu 16.04


© 2022 Ashwin Nanjappa • All writing under CC BY-SA license • 🐘 @codeyarns@hachyderm.io📧