I can't believe it took me years to figure this out, but you can very easily set the pprint.pprint
function as the default implementation to use when calling print()
during the invocation of a shell or when running a program under the python interpreter.
The trick involves overriding the default for sys.displayhook
and pointing it to pprint.pprint
:
import sys
import pprint
sys.displayhook = pprint.pprint
Once displayhook
is set, any invocation of print()
within the same interpreter context will defer the output formatting to pprint.pprint
.
If you'd like to have that as your default without having to override sys.displayhook
in all your scripts and applications, you can create a sitecustomize.py
or usercustomize.py
module somewhere on your $PYTHONPATH
that will be picked up when the python interpreter start up.