1
2
3
import pdb
from pdb import set_trace as bp
bp()

Debugging Python with pdb 🐞

Console pdb

Debugging can be quite messy and annoying but pdb helps out. pdb is an out-of-the-box fully featured Python console to make use of any Python command. If you have a standard script to debug simply use

1
python -m pdb your_script.py

In-Code pdb

Unfortunately this doesn't work for Python packages that you call via path variable, such as Instagram-Scraper. In this case either simply look for the respective file in your folder. For Instagram-Scraper on Ubuntu it would be:

1
/home/user/miniconda3/lib/python3.7/site-packages/instagram_scraper/app.py

Either put the following lines at the very beginning to start pdb right away or look for the lines you want to debug:

1
2
3
import pdb
from pdb import set_trace as bp
bp()

Save the file and execute as usual. When the code runs into bp() function you will get pdb console. Here Python oneliners come in handy!

Export string to file in one line

1
open("out_file.txt", 'a').write(variable)

Otherwise just type interact and you have a fully-featured Python console for statements over multiple lines. When finished, type quit() and you get back to pdb.