Using a debugger¶
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.
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 command to navigate the code and the python debugger:
help: prints the help
help n: prints help about command
n(ext): executes one line of code. Executes and steps over functions.
s(tep): step into current function in line of code
l(ist): list program around current position
w(where): prints current stack (where we are in code)
[enter]: repeats last command
anypythonvariable: print the value of that variable
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.
Break points tell debugger where and when to stop We say
%%writefile solutions/diffusionmodel/energy_example.py from diffusion_model import energy print(energy([5, 6, 7, 8, 0, 1]))
The debugger is, of course, most 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.
%%bash python -m pdb solutions/diffusionmodel/energy_example.py < commands
> /home/runner/work/rsd-engineeringcourse/rsd-engineeringcourse/ch03tests/solutions/diffusionmodel/energy_example.py(1)<module>() -> from diffusion_model import energy (Pdb) Restarting /home/runner/work/rsd-engineeringcourse/rsd-engineeringcourse/ch03tests/solutions/diffusionmodel/energy_example.py with arguments: # restart session > /home/runner/work/rsd-engineeringcourse/rsd-engineeringcourse/ch03tests/solutions/diffusionmodel/energy_example.py(1)<module>() -> from diffusion_model import energy (Pdb) > /home/runner/work/rsd-engineeringcourse/rsd-engineeringcourse/ch03tests/solutions/diffusionmodel/energy_example.py(2)<module>() -> print(energy([5, 6, 7, 8, 0, 1])) (Pdb) Breakpoint 1 at /home/runner/work/rsd-engineeringcourse/rsd-engineeringcourse/ch03tests/solutions/diffusionmodel/diffusion_model.py:2 (Pdb) > /home/runner/work/rsd-engineeringcourse/rsd-engineeringcourse/ch03tests/solutions/diffusionmodel/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
Debugging when something goes wrong:
- Have a crash somewhere in the code
python -m pdb file.pyor run the cell with
The program should stop where the exception was raised
lfor position in code and in call stack
downto navigate up and down the call stack
- 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)