Debugger and Profiler

The Debugger

Pdb is an interactive Python source-code debugger available as a standard library module.

Top-Level Interface

import pdb

Loads debugger interface (a module written in Python). [,globals [,locals]])

Runs code string string in the debugger. The string is assumed to contain a Python statement and runs in module __main__ by default unless globals and/or locals namespace dictionaries are passed.

pdb.runeval(string [,globals [,locals]])

Like, but assumes string contains an expression (not a statement) and returns its result.

pdb.runcall(func, arg1, arg2, . . . )

Runs a call to function object func with the listed arguments (in the debugger). Returns func result. )

Runs postmortem session on the last exception that occurred (much like debugging a core file).


Runs a postmortem session on traceback object tb.


A customizable debugger class which retains state between operations (e.g., break points).


The debugger runs program until breakpoint reached, exception raised, or end of program, but it also stops initially so you can set break points and continue.

In run, string can be any Python statement, in the form of a Python string:“x = main(‘spam’)”).

To debug script files, import them, optionally setting sys.argv first:“import file”). Can also run scripts under debugger with a command line like: “python libpath/”

Interactive Commands

Each command name has a short (h) and long (help) ...

Get Python Pocket Reference now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.