Another Option:
The module pdb defines an interactive source code debugger for Python programs. It
supports setting (conditional) breakpoints and single stepping at the source line level,
inspection of stack frames, source code listing, and evaluation of arbitrary Python code
in the context of any stack frame. It also supports post-mortem debugging and can be
called under program control.
The debugger is extensible — it is actually defined as the class Pdb. This is currently
undocumented but easily understood by reading the source. The extension interface
uses the modules bdb and cmd.
The debugger’s prompt is (Pdb). Typical usage to run a program under control of the
debugger is:
>>> import pdb
>>> import mymodule
>>> pdb.run(‘mymodule.test()’)
> (0)?()
(Pdb) continue
> (1)?()
(Pdb) continue
NameError: ‘spam’
> (1)?()
(Pdb)
pdb.py can also be invoked as a script to debug other scripts. For example:
python -m pdb myscript.py
The typical usage to break into the debugger from a running program is to insert
import pdb; pdb.set_trace()
There are a few commands we can use with pdb are as follows.
Some useful ones to remember are:
• b: set a breakpoint
• c: continue debugging until you hit a breakpoint
• s: step through the code
• n: to go to next line of code
• l: list source code for the current file (default: 11 lines including the line being
executed)
• u: navigate up a stack frame
• d: navigate down a stack frame
• p: to print the value of an expression in the current context
AssignmentTutorOnline