21

I find myself doing this in pdb quite often:

import pprint
pprint.PrettyPrinter().pprint(variable_of_interest)

Is there a better way to pretty-print variables from pdb? I'm looking for something easier to type, and ideally something that's always available in pdb so I can use it anytime I'm debugging.

nonagon
  • 3,271
  • 1
  • 29
  • 42

4 Answers4

49

In pdb documentation at the section Debugger Commands:

pp expression

Like the p command, except the value of the expression is pretty-printed using the pprint module.

Community
  • 1
  • 1
enrico.bacis
  • 30,497
  • 10
  • 86
  • 115
6

Like @enrico.bacis has mentioned pp can be used for pretty printing while using pdb.

(Pdb) pp your_var
mrprofessor
  • 101
  • 1
  • 10
3

Once you are in (Pdb) shell:

(Pdb)$ import pprint

# then you can pretty print your variables.
(Pdb)$ pp( locals() )
(Pdb)$ pp(request.data)

Also, I recommend using the new pdb alternative breakpoint(), that way you don't have to import pdb package in any of your dev environment sourcecode. It's also easier to type than pdb.set_trace()

Kermit
  • 4,922
  • 4
  • 42
  • 74
-1

Here is how I am doing it:

import pdb
import pprint

"""
Debugging views
pdb.set_trace()

With pretty-print
pp(my_var)
"""
def my_view(request):
    pdb.set_trace()
    # rest of view

Then in debugger:

> (Pdb) pp(my_var)

Although, I would like to know how to edit the pdb settings to change p to pp

Kermit
  • 4,922
  • 4
  • 42
  • 74