Using the Python Debugger (pdb)

From Training Material
Jump to navigation Jump to search


Interrupting program execution

You can interrupt the program execution and start the debugger by inserting these commands at any point:

import pdb
pdb.set_trace()

The debugger shows a shell that works like the normal Python command line, but with some extra commands:

  • n (next) – execute next statement.
  • s (step) – execute next statement, and descend into functions.
  • l (list) – show source code.
  • c (continue) – continue execution until the next breakpoint.
  • help – print help message.
  • q (quit) – abort the program.

Breakpoints

  • The command 'b <line number>' sets a breakpoint at the given line
  • The command 'b' displays all breakpoints set

Tips

  • A valuable diagnostic tool is to use the debugger for manipulate variable values. e.g.:
>>> a = 1
  • Most IDE's are supporting the debugger. It is much more comfortable to control the program from there.
  • In the debugger command line you can get help on any command by typing 'help <command>'.