Debugging Python programs
There are many IDEs out there that make it a breeze to debug python. But one day you might find yourself in a situtation where you do not have access to this fancy IDE of yours, and you’re trying to debug a program from the interpreter.
In a file named crash.py
, have the following toy function:
Now let’s run this with an argument which is not iterable (like an integer):
Okay - so it crashes as expected. Let’s debug!
a
shows the local variables - here we clearly see l
is set to 1. But what if we wanted to step through? Well we can’t quite do that once the program has been terminated so let’s re-run it with pdb
from the start.
We’re now at the very top of the program. Before continuing, let’s set a breakpoint on the for
statement:
We can set we hit the breakpoint (denoted by ‘B’), and we use l
to list the current source. Now that we’re in a debug session, let’s make l
an actual list.
Right that was cheeky - pdb
interprets l
as the list command. We need to explicitly tell pdb
it’s a python command, and we do that by prefixing it with !
. And we continue execution
Press c
to continue stepping through this, or cl
to clear all breakpoints and r
to resume execution.
This only touches the surface and there’s much more pdb
can do. We’ve been spoilt by GUI-based debuggers and you’ll probably never need the above. But one day it just might get you out of an undesireable situation ^_^