TIL: Custom alias for pretty printing in Python debugger with .pdbrc (including Django models!)

June 17, 2025 • TIL, Python, productivity

I've typed `import rich; rich.print(<the thing>)` too many times in the Python debugger. Here is how you can set up a custom alias to make this way easier!

A colleague recently shared this cool trick of adding custom aliases for the Python debugger by creating a ~/.pdbrc file in your home directory.

For example to pretty print any Python object using rich, just add this line:

alias rp import rich; rich.print(%*)

Note: You might have to add rich to your projects dependencies, for example uv add rich --dev.

Now anytime you're using a breakpoint() in your code, you can inspect any object like this:

rp my_object

Here is a screenshot of pretty printing a Django view context object:

RecipeDetailView context

Here is another alias for inspecting Django model instances, using model_to_dict().

alias rpd import rich; from django.forms import model_to_dict; rich.print(model_to_dict(%*))

Here is how it looks like:

Recipe Giant cheese on toast

May you enjoy pretty printed objects in your Python debugger!


Edit 2025-06-19

James Beith posted an adapted version of this that includes printing a useful reminder that this alias exists. He also shows how to add a similar alias to the IPython startup script.


Recent posts