Python Debugging
Python debugging can be accomplished by one of the following ways. Some of which are terminal based, whilst some of them being GUI based. However each of these provide pdb to interact and query objects
- pdb / breakpoint()
- pudb
- web_pdb
- vscode
pdb
# Set up a breakpoint
import pdb; pdb.set_trace()
# Since python 3.7 onwards,
# instead of wrtiting the cumbersome import pdb; pdb.set_trace(),
# one can simply use the breakpoint()
breakpoint()
However, with usage of breakpoint(), there is a neat little trick, using PYTHONBREAKPOINT=0 breakpoint() calls can be skipped.
PYTHONBREAKPOINT=0 python3 <your-script>.py
pudb
$ pip install pudb
# Set up a breakpoint
import pudb; pudb.set_trace()
web_pdb (GUI based)
$ pip install web-pdb
# Set up a breakpoint
import web_pdb; web_pdb.set_trace()
# OR, use breakpoint() using PYTHONBREAKPOINT
# PYTHONBREAKPOINT='web_pdb.set_trace' python3 debug_web_pdb.py
breakpoint()
# Once code hits the breakpoint
# Open http://localhost:5555
PYTHONBREAKPOINT='web_pdb.set_trace' python3 <your-script>.py
vscode (GUI based)
Set code breakpoint in VSCode editor GUI
Run the program in debug mode (F5)