Woman (Marguerite) taking a selfie in the mirror, phone in hand, wearing a RHCP shirt.

Marguerite Whitford

Senior Product Engineer @Parse.ly

tldr;

Import pprint, pause code with pdb, poke around. This module allows us to debug and log data in a readable format.

What is pprint?

pprint is a “pretty print” module that allows you to view complex data structures in an easy-to-read format. With a dictionary that’s several levels deep, for example, using regular print to debug it can quickly become overwhelming. With pprint, you can see the structure of the dictionary clearly, making it much easier to understand what’s going on.

To use pprint, simply import it and call it on the data structure you want to view:

Instead of the minified chunk of data getting spit out into the terminal, pprint formats it into a readable shape.

Becomes:

Much better.

What is pdb?

pdb, the Python debugger, is another tool that I find myself using all the time. It allows you to pause your code at any point and interactively debug it. You can inspect variables, step through code, and even change values on the fly. This can be incredibly useful for tracking down bugs in your code.

To use pdb, simply import it and add the pdb.set_trace() method at the point where you want to start debugging:

Once your code hits the set_trace() line, it will pause execution and drop you into a command prompt. From there, you can type commands to inspect variables, step through code, and more.

pprint and pdb shorthand

In my VS Code User Snippets, I keep a shortcut of pp mapped to the following:

These two lines will import pprint and set the pdb pause in one fell swoop, saving us all countless milliseconds.

All credit goes to my coworker, Alex Ose, for turning me onto this module.

Note: If you are in an environment where your node and django servers are run together in a single node command, pprint will eat all logs. This works best when both servers are running independently.