Using a debugger

Please open notebook rsepython-s2r5.ipynb

Stepping through the code

Debuggers are programs that can be used to test other programs. They allow programmers to suspend execution of the target program and inspect variables at that point.

Using the python debugger

Unfortunately this doesn’t work nicely in the notebook. But from the command line, you can run a python program with:

python -m pdb my_program.py

Basic navigation:

Basic command to navigate the code and the python debugger:

The python debugger is a python shell: it can print and compute values, and even change the values of the variables at that point in the program.

Breakpoints

Break points tell debugger where and when to stop

We say:

%%writefile energy_example.py
from diffusion.model import energy
print(energy([5, 6, 7, 8, 0, 1]))

Writing energy_example.py

The debugger is, of course, mostly used interactively, but here I’m showing a prewritten debugger script:

%%writefile commands
restart  # restart session
n
b energy # program will stop when entering energy
c        # continue program until break point is reached
print density # We are now "inside" the energy function and can print any variable.

Writing commands

%%bash
python -m pdb energy_example.py < commands

/mnt/c/Users/jhetherington.TURING/devel/rsd-engineeringcourse/ch03tests/energy_example.py(1)() </span>

-> from diffusion.model import energy

(Pdb) Restarting energy_example.py with arguments:

# restart session

> /mnt/c/Users/jhetherington.TURING/devel/rsd-engineeringcourse/ch03tests/energy_example.py(1)() </span>

-> from diffusion.model import energy

(Pdb) > /mnt/c/Users/jhetherington.TURING/devel/rsd-engineeringcourse/ch03tests/energy_example.py(2)() </span>

-> print(energy([5, 6, 7, 8, 0, 1]))

(Pdb) Breakpoint 1 at /mnt/c/Users/jhetherington.TURING/devel/rsd-engineeringcourse/ch03tests/diffusion/model.py:3

(Pdb) > /mnt/c/Users/jhetherington.TURING/devel/rsd-engineeringcourse/ch03tests/diffusion/model.py(9)energy()

-> from numpy import array, any, sum

(Pdb) [5, 6, 7, 8, 0, 1]

(Pdb)

Alternatively, break-points can be set on files: b file.py:20 will stop on line 20 of file.py.

Post-mortem

Debugging when something goes wrong:

  1. Have a crash somewhere in the code
  2. run python -m pdb file.py or run the cell with %pdb on

The program should stop where the exception was raised

  1. use w and l for position in code and in call stack
  2. use up and down to navigate up and down the call stack
  3. inspect variables along the way to understand failure

This does work in the notebook.

%pdb on
from diffusion.model import energy
partial_derivative(energy,[5,6,7,8,0,1],5)

Next: Reading - Continuous Integration