// I need to

ModernizeUpgrade without breaking it RescueFix what someone else started Build NewTurn an idea into a product Reach More PeopleGet found, get used, include everyone

// Company

Case StudiesExpertiseAbout + CareersResourcesContact Book a call →
≡articleRescue

PdbpPlease!

pdbpp is a drop-in replacement for Python's pdb debugger that makes debugging much nicer. A quick refresher on pdb commands and breakpoints to start.

Art+Logic Sep 25, 20133 min read

From the Art+Logic archive · written in 2013. The tools may have moved on; the thinking still holds.

pdbpp is an awesome module that replaces pdb and makes debugging way nicer in Python. https://pypi.python.org/pypi/pdbpp/ A debugger is an extremely useful tool. I used to spend a lot of time with pdb. Then there was pdbpp and it has made debugging a much much nicer experience. For those who are not familiar with pdb, it’s a module you can use to set breakpoints in your Python code and then step around; execute Python; set further breakpoints etc. given:

def doubleVals(d):
    newD = {}
    for key, val in d.iteritems():
        newD[key] = val * 2
    return newD
import pdb; pdb.set_trace()
a = {'a': 1, 'b':2}
a = doubleVals(a)
a.update({'a': 3, 'c': 4})
print a

You can do this sort of thing: Software screen capture

$ python example.py
<blockquote>
  /home/fran/random/pdbpp_example/example.py(8)()
  -> a = {'a': 1, 'b':2}
  (Pdb) n
  /home/fran/random/pdbpp_example/example.py(9)()
  -> a = doubleVals(a)
  (Pdb) s
  --Call--
  /home/fran/random/pdbpp_example/example.py(1)doubleVals()
  -> def doubleVals(d):
  (Pdb) l
    1  -> def doubleVals(d):
    2         newD = {}
    3         for key, val in d.iteritems():
    4             newD[key] = val * 2
    5         return newD
    6
    7     import pdb; pdb.set_trace()
    8     a = {'a': 1, 'b':2}
    9     a = doubleVals(a)
   10     a.update({'a': 3, 'c': 4})
   11     print a
  (Pdb) n
  /home/fran/random/pdbpp_example/example.py(2)doubleVals()
  -> newD = {}
  (Pdb)
  /home/fran/random/pdbpp_example/example.py(3)doubleVals()
  -> for key, val in d.iteritems():
  (Pdb)
  /home/fran/random/pdbpp_example/example.py(4)doubleVals()
  -> newD[key] = val * 2
  (Pdb)
  /home/fran/random/pdbpp_example/example.py(3)doubleVals()
  -> for key, val in d.iteritems():
  (Pdb) break 10
  Breakpoint 1 at /home/fran/random/pdbpp_example/example.py:10
  (Pdb) c
  /home/fran/random/pdbpp_example/example.py(10)()
  -> a.update({'a': 3, 'c': 4})
  (Pdb) print a
  {'a': 2, 'b': 4}
  (Pdb) n
  /home/fran/random/pdbpp_example/example.py(11)()
  -> print a
  (Pdb)
  {'a': 3, 'c': 4, 'b': 4}
  --Return--
  /home/fran/random/pdbpp_example/example.py(11)()->None
  -> print a
  (Pdb) c

and

<blockquote>
  /home/fran/random/pdbpp_example/example.py(8)()
  -> a = {'a': 1, 'b':2}
  (Pdb) n
  /home/fran/random/pdbpp_example/example.py(9)()
  -> a = doubleVals(a)
  (Pdb) s
  --Call--
  /home/fran/random/pdbpp_example/example.py(1)doubleVals()
  -> def doubleVals(d):
  (Pdb) l
    1  -> def doubleVals(d):
    2         newD = {}
    3         for key, val in d.iteritems():
    4             newD[key] = val * 2
    5         return newD
    6
    7     import pdb; pdb.set_trace()
    8     a = {'a': 1, 'b':2}
    9     a = doubleVals(a)
   10     a.update({'a': 3, 'c': 4})
   11     print a
  (Pdb) w
    /home/fran/random/pdbpp_example/example.py(9)()
  -> a = doubleVals(a)
  /home/fran/random/pdbpp_example/example.py(1)doubleVals()
  -> def doubleVals(d):
  (Pdb) u
  /home/fran/random/pdbpp_example/example.py(9)()
  -> a = doubleVals(a)
  (Pdb) l
    4             newD[key] = val * 2
    5         return newD
    6
    7     import pdb; pdb.set_trace()
    8     a = {'a': 1, 'b':2}
    9  -> a = doubleVals(a)
   10     a.update({'a': 3, 'c': 4})
   11     print a
  [EOF]
  (Pdb) print a
  {'a': 1, 'b': 2}
  (Pdb) d
  /home/fran/random/pdbpp_example/example.py(1)doubleVals()
  -> def doubleVals(d):
  (Pdb) print d
  {'a': 1, 'b': 2}

Where ‘n’ steps to the next line. ‘s’ steps in. ‘l’ lists around the current line of execution. ‘break’ adds breakpoints (in this case at line 10 in the module). ‘c’ continues until the next break. Hitting enter repeats whatever you did last. You can execute Python from the prompt so you can print things, or import pprint and pretty print them, or do anything you like. Examine the call stack with ‘w’ (where). Move up or down in the call stack with ‘u’ and ‘d’. pdbpp does all of this and more! Install with pip and do nothing else and now every time you hit a breakpoint you get pdbpp

For me the best things about pdbpp are the tab completion, ‘ll’ and the syntax highlighting. It also does other handy things like ‘source’ and ‘display’:

(Pdb++) source doubleVals
   1     def doubleVals(d):
   2         newD = {}
   3         for key, val in d.iteritems():
   4             newD[key] = val * 2
   5         return newD
(Pdb++) display a
(Pdb++) n
<blockquote>
  /home/fran/random/pdbpp_example/example.py(9)()
  -> a = doubleVals(a)
  a:  --> {'a': 1, 'b': 2}
  (Pdb++) n
  /home/fran/random/pdbpp_example/example.py(10)()
  -> a.update({'a': 3, 'c': 4})
  a: {'a': 1, 'b': 2} --> {'a': 2, 'b': 4}
  (Pdb++)

(these and more are described on the pypi page). A debugger is a useful tool and you should use good tools. Check it out.

keep going

More to explore in Rescue.

All Rescue resources →
let's talk

Got a problem like this one?

Tell us what's stuck. A real person replies within one business day.

Talk to our team →

Talk to people who know software, not a sales script.

Your first call is with Aja, our VP of Sales. He'll help make sure you're solving the right problem. Here's what you'll cover together:

  1. What you're trying to accomplish

    The business goal first, then the technology.

  2. Possible approaches

    And the tradeoffs of each, in plain language.

  3. Risks you may not have considered

    Technical and business, before they cost you.

  4. Whether AI changes the math

    What's practical, and what's just hype for your project.

  5. Sensible next steps

    Even if the right next step is not us.

book-a-call.ics

Loading the scheduler…

Open the booking page →

Some problems are easier to explain in writing. Send us a few lines about what you're working on, and we'll reply within one business day. Helpful to include:

  1. What the software does, or should do
  2. What's stuck, or what's worrying you
  3. Any deadline or budget you have in mind
note.md

Loading the form…