A debugger is a program that allows you to follow your code as it runs. You run your code line-by-line and see exactly what is going on. This is useful for fixing bugs. It is also useful for understanding what is going on with code.


Install pdb++ using

pip install pdbpp

Trying it out

Create a file called that looks like:

import pdb

numbers = range(5)

for num in numbers:
    newnumber = modify_number(num)
    print newnumber

def modify_number(num):
    return 3 * num

Then, from the command line type python Python will then start interpreting this file (as it always does). When it gets to the line pdb.set_trace() the debugger will "hook" (stop execution of your program and display the position you are at). You should see a syntax-highlighted snapshot of your code. Type sticky and you will see a display of all your code. Type next or n to go to the next line. Type step or s to step into the function modify_number (do this when you are over that line). At any point you can print out the contents of a variable by typing the name of the variable. You can quit with q (unless you have a variable named q, in which case use !!q. To see a full display of commands type help. Also, check out this website.


Finally, you can customize pdb++ by creating a file in your home directory. Mine looks like:

import readline
import pdb

class Config(pdb.DefaultConfig):

    stdin_paste = 'epaste'
    sticky_by_default = True

    def __init__(self):
        readline.parse_and_bind('set convert-meta on')
        readline.parse_and_bind('Meta-/: complete')

    def setup(self, pdb):
        Pdb = pdb.__class__
        Pdb.do_l = Pdb.do_longlist
        Pdb.do_st = Pdb.do_sticky

blog comments powered by Disqus


04 February 2013


Subscribe to RSS Feed