The Python Debugger

pdb The module pdb defines an interactive source code debugger for Python programs. It supports setting 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 a class Pdb. This is currently undocumented but easily understood by reading the source. The extension interface uses the (also undocumented) modules bdb and cmd. Pdb bdb cmd A primitive windowing version of the debugger also exists — this is module wdb, which requires STDWIN (see the chapter on STDWIN specific modules). wdb 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()')
> <string>(0)?()
(Pdb) continue
> <string>(1)?()
(Pdb) continue
NameError: 'spam'
> <string>(1)?()
(Pdb)
Typical usage to inspect a crashed program is:
>>> import pdb
>>> import mymodule
>>> mymodule.test()
Traceback (innermost last):
  File "<stdin>", line 1, in ?
  File "./mymodule.py", line 4, in test
    test2()
  File "./mymodule.py", line 3, in test2
    print spam
NameError: spam
>>> pdb.pm()
> ./mymodule.py(3)test2()
-> print spam
(Pdb)
The module defines the following functions; each enters the debugger in a slightly different way:
\begin{funcdesc}{run}{statement\optional{\, globals\optional{\, locals}}}
Execut...
...he \code{exec} statement or the \code{eval()}
built-in function.)
\end{funcdesc}

\begin{funcdesc}{runeval}{expression\optional{\, globals\optional{\, locals}}}
E...
...e
expression. Otherwise this function is similar to
\code{run()}.
\end{funcdesc}

\begin{funcdesc}{runcall}{function\optional{\, argument\, ...}}
Call the \var{fu...
.... The debugger prompt appears as
soon as the function is entered.
\end{funcdesc}

\begin{funcdesc}{set_trace}{}
Enter the debugger at the calling stack frame. Thi...
...e
is not otherwise being debugged (e.g. when an assertion fails).
\end{funcdesc}

\begin{funcdesc}{post_mortem}{traceback}
Enter post-mortem debugging of the given \var{traceback} object.
\end{funcdesc}

\begin{funcdesc}{pm}{}
Enter post-mortem debugging of the traceback found in
\code{sys.last_traceback}.
\end{funcdesc}


Subsections