Linux/Unix > Python Essential Reference, Second Edition > A. The Python Library > The Python Debugger | See All Titles

< BACK | Make Note | Bookmark | CONTINUE >

155117184014003188065099048180054212144238241179195140058238111161105094192220113220094088

### The Python Debugger

The Python debugger is loaded by importing the `pdb` module. The `pdb` module provides an interactive source code debugger that allows post-mortem debugging, inspection of stack frames, breakpoints, single stepping of source lines, and code evaluation.

The debugger is started by loading the `pdb` module and issuing one of the following functions:

    run(statement [, globals [, locals]])

Executes the string `statement` under debugger control. The debugger prompt will appear immediately before any code executes. Typing `'continue'` will force it to run. `globals` and `locals` define the global and local namespaces in which the code runs.

    runeval(expression [, globals [, locals]])

Evaluates the `expression` string under debugger control. The debugger prompt will appear before any code executes, as with `run()`. On success, the value of the expression is returned.

    runcall(function [, argument, ...])

Calls a function within the debugger. `function` is a callable object. The debugger prompt will appear before any code executes. The return value of the function is returned upon completion.

    set_trace()

Starts the debugger at the point at which this function is called. This can be used to hard-code a debugger breakpoint into a specific code location.

    post_mortem(traceback)

Starts post-mortem debugging of a traceback object.

    pm()

Enters post-mortem debugging using the traceback in `sys.last_traceback`.

When the debugger starts, it will present a prompt such as the following:

    >>> import pdb
    >>> import buggymodule
    >>> pdb.run('buggymodule.start()')
    > <string>(0)?()
    (Pdb)

`(Pdb)` is the debugger prompt at which the following commands are recognized. Note: Some commands have a short and a long form. In this case, parentheses are used to indicate both forms. For example, `h(elp)` means that either `h` or `help` is acceptable.

    h(elp) [command]

Shows the list of available commands. Specifying a command returns help for that command.

    w(here)

Prints a stack trace.

    d(own)

Moves the current frame one level down in the stack trace.

    u(p)

Moves the current frame one level up in the stack trace.

    b(reak) [loc [, condition]]

Sets a breakpoint at location `loc` . `loc` is one of the following:

Setting | Description
--- | ---
`n` | A line number in the current file
`filename` `:``n` | A line number in another file
`function` | A function name in the current file
`filename` `:``function` | A function name in another file

If `loc` is omitted, all the current breakpoints are printed. `condition` is an expression that must evaluate to true before the breakpoint is honored.

    tbreak [loc [, condition]]

Sets a temporary breakpoint that’s removed after its first hit.

    cl(ear) [bpnumber [bpnumber...]]

Clears a list of breakpoint numbers. If breakpoints are not specified, all breaks are cleared.

    disable [bpnumber [bpnumber...]]

Disables the set of specified breakpoints. Unlike with `clear`, they can be reenabled later.

    enable [bpnumber [bpnumber...]]

Enables a specified set of breakpoints.

    ignore bpnumber [count]

Ignores a breakpoint for `count` executions.

    condition bpnumber [condition]

Places a condition on a breakpoint. `condition` is an expression that must evaluate to true before the breakpoint is recognized. Omitting the condition clears any previous condition.

    s(tep)

Executes a single source line and stops inside called functions.

    n(ext)

Executes until the next line of the current function. Skips the code contained in function calls.

    r(eturn)

Runs until the current function returns.

    c(ont(inue))

Continues execution until the next breakpoint is encountered.

    l(ist) [first [, last]]

Lists source code. Without arguments, lists 11 lines around the current line. With one argument, lists 11 lines around that line. With two arguments, lists lines in a given range. If `last` is less than `first` , it’s interpreted as a count.

    a(rgs)

Prints the argument list of the current function.

    p expression

Evaluates the expression in the current context and prints its value.

    alias [name [command]]

Creates an alias called `name` that executes `command` . The substrings `'%1'`, `'%2'`, and so forth are replaced by parameters when the alias is typed. `'%*'` is replaced by all parameters. If no command is given, the current alias list is shown. Aliases can be nested and can contain anything that can be legally typed at the `Pdb` prompt. For example:

    #Print instance variables (usage "pi classInst")
    alias pi for k in %1._ _dict_ _.keys(): print "%1.",k,"=",%1._ _dict_ _[k]
    #Print instance variables in self
    alias ps pi self

    unalias name

Deletes the specified alias.

    [!] statement

Executes the (one-line) `statement` in the context of the current stack frame. The exclamation point can be omitted unless the first word of the statement resembles a debugger command. To set a global variable, you can prefix the assignment command with a `"global"` command on the same line:

    (Pdb) global list_options; list_options = ['-l']
    (Pdb)

    q(uit)

Quits from the debugger.

#### Notes

- Entering a blank line repeats the last command entered.

- Commands that the debugger doesn’t recognize are assumed to be Python statements and are executed in the context of the program being debugged.

- If a file `.pdbrc` exists in the user’s home directory or in the current directory, it’s read in and executed as if it had been typed at the debugger prompt.

| Last updated on 3/28/2002 Python Essential Reference, Second Edition, © 2002 New Riders Publishing

< BACK | Make Note | Bookmark | CONTINUE >

© 2002, O'Reilly & Associates, Inc.