← Back | Deepak Cherian | 26 Aug 2019

Debugging tips for python

Here are some useful debugging tips I have learned. Please email me if any of the text can be clarified or if you know of other (or better) methods.

Entering and using a debugger prompt

  1. Entering a debugger:
    1. Python script: import IPython; IPython.core.debugger.set_trace() is the IPython equivalent of MATLAB's keyboard. You can add this line in a python script and the script's execution will be paused at that line giving you an ipdb prompt.
    2. Python script: import pdb; pdb.set_trace() is the pdb equivalent of the above.
    3. Jupyter notebook: The above commands can be used within functions defined in a Jupyter notebook and will also work when calling external functions from a notebook. If you want to debug an error raised in a Jupyter notebook, use the %debug magic in a new cell to drop into an debugger prompt that will let you inspect the program's state.
    4. pytest: pytest --pdb test.py will drop you into a pdb prompt when a test fails.
  2. Many commands to inspect your programs state are available at a debugger prompt . You can use up , down and other commands to navigate the stack. Type h or help to see all available commands. Usually, I end up type up or u to navigate up the stack to a function call I recognize, inspect the arguments of that function call (see below) and then move down (or d) if needed.
  3. If you have variables with the same names as the debugging commands (e.g. args), you will need to use locals()['args'] to inspect the value of variable args (since locals() is a dict containing a mapping of local variable names to their respective objects, see docs).
  4. You can make interactive matplotlib plots at a debugging prompt but no drawing will actually happen unless you also call plt.pause(1). import matplotlib.pyplot as plt is required prior to running plt.pause. See this Github comment for details.

Useful links

Creative Commons Attribution License Creative Commons Attribution License

This work is licensed under a Creative Commons Attribution 4.0 International License.