Debugging in Python

Posted by Bruce Liu on March 8, 2021 , updated on August 5, 2022

🏷️ Tags: Python , pdb , pudb

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 of import 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

Python tools

Share on:

« PREVIOUS: Calculator for loan and repay
NEXT: Academic journals on climate change »