Debugging in Python
pdb - the Python Debugger
-
Invoke pbd from terminal to debug the entire script.
python -m pdb mytest.py
-
Insert
import pdb; pdb.set_trace()
in a specific line of the script. -
Insert
breakpoint()
in the script. The built-in breakpoint() is a equivalent way ofimport pdb; pdb.set_trace()
. -
Commands: c(ontinue), s(tep), n(ext), r(eturn), j(ump), q(uit) .. more.
PuDB - a console-based visual debugger for Python
Installing
Install PuDB by pip install pudb
or conda install -c conda-forge pudb
.
Usage
Similar to pdb,
-
Debug the entire script with:
pudb mytest.py
or
python -m pudb mytest.py
-
Insert in a specific line with:
from pudb import set_trace; set_trace()
or
import pudb; pu.db
-
Integrate pudb with breakpoint() function. Add:
export PYTHONBREAKPOINT="pudb.set_trace"
in your .bashrc or .zshrc. Then use
breakpoint()
in your script. -
Commands: tyep ‘?’ in the console to check the conmand list.
References
- pdb — The Python Debugger
- Getting Started: pudb 2020.1 dicumentation
- Python 3.7’s new builtin breakpoint — a quick tour
Share on: