Skip to content

Debugging with pdb

phlax edited this page Oct 25, 2016 · 4 revisions

Run pootle

To use pdb you will need to run pootle in the foreground

pootle runserver

You can also use pdb in tests or commands, or any other code that runs from the console.

Add a breakpoint

You can add a breakpoint anywhere in python code

def some_function():
    do_something()
    import pdb; pdb.set_trace()
    do_something_else()

When this code is run the python interpreter will stop whenever it reaches this line of code, and allow you to interact with the program from the console where pootle is being run.

If you are expecting the code to be triggered from a view, open the corresponding web page, and then switch back to your console.

Basic commands

  • l: list the lines of code surrounding the breakpoint (you can use eg l 20 to view lines around line 20)
  • n: execute the next line of code
  • c: continue program execution
  • s: step "into" the next line of code
  • q: quit the breakpoint

in most cases ctrl-c will execute the program altogether

Finding the calling code

You can find what code called a particularly method or function using inspect

When doing this inside pdb - you need to move up the stack beyond the pdb code

import inspect
inspect.stack()[10]
Clone this wiki locally