Registry / serialization / prettyprinter

prettyprinter

JSON →
library0.18.0pypypi✓ verified 24d ago

PrettyPrinter is a Python library (version 0.18.0) that offers syntax-highlighting, declarative, and composable pretty printing for Python 3.5+ data structures. It aims to be a feature-rich alternative to Python's standard `pprint` module, providing improved readability, customizability, and integrations with other libraries. Its release cadence has been somewhat irregular, but the 0.18.0 version released in 2019 has been stable, suggesting a mature library.

pip install prettyprinter
INSTALL
IMPORT
SIG · PRETTYPRINTER
P
prettyprinter
serializationpythonv0.18.0
Install
2.4s avg
Import
130ms
Disk
26MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.18.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.132s · 27.6MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 2.4s · import 0.128s · 28MB
26MB installed
● package 26MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

pprint
from prettyprinter import pprint
import pprint # This imports the standard library module
To use prettyprinter's main function, explicitly import `pprint` from `prettyprinter` to avoid conflict with the standard library's `pprint` module.
install_extras
from prettyprinter import install_extras
Used for enabling integrations with shells (like IPython) or other third-party libraries (e.g., Django, NumPy, Requests).
register_pretty
from prettyprinter import register_pretty, pretty_call
These functions are used when defining custom pretty-printing rules for your own Python types.

This quickstart demonstrates basic pretty-printing of a nested dictionary and how to define custom pretty-printing rules for your own classes using `@register_pretty`. It also notes how to enable shell integrations for a more persistent pretty-printing experience.

from prettyprinter import pprint, register_pretty, pretty_call # Basic pretty-printing of a nested dictionary data = { 'name': 'PrettyPrinter Example', 'details': { 'id': 12345, 'items': [ {'item_id': 'A1', 'value': 100.5, 'tags': ['new', 'sale']}, {'item_id': 'B2', 'value': 200, 'tags': ['old']}, {'item_id': 'C3', 'value': 50.75, 'tags': []} ], 'status': 'active' }, 'configs': [ {'key': 'timeout', 'value': 30}, {'key': 'retries', 'value': 5} ] } print('--- Standard print ---') print(data) print('\n--- PrettyPrinter output ---') pprint(data) # Custom pretty-printing for a user-defined class class MyObject: def __init__(self, name, values): self.name = name self.values = values @register_pretty(MyObject) def pretty_my_object(value, ctx): return pretty_call( ctx, MyObject, name=value.name, values=value.values ) my_instance = MyObject('CustomItem', [{'x': 1, 'y': 2}, {'z': 3}]) print('\n--- Custom object pretty-printing ---') pprint(my_instance) # To enable shell integration (e.g., for `ipython` or default python shell): # from prettyprinter import install_extras # install_extras(['ipython']) # or ['python'] # {'a': 1, 'b': 2} # will now be pretty-printed in the shell
Debug
Known issues
breakingThe default behavior for dictionary key sorting changed in version 0.8.0. Previously, keys were sorted like in the standard library's `pprint` module. Now, they default to insertion order (for CPython 3.6+). To revert to sorted keys, use the `sort_dict_keys` argument in `pprint` or configure it in `PrettyContext` for custom printers.
fix
If sorted dictionary keys are required, explicitly pass `sort_dict_keys=True` to `pprint()` or `pformat()`, or set this option in your custom `PrettyContext`.
affects: 0.8.0 and later
gotchaThere are two distinct Python libraries with similar names: the built-in `pprint` module and the external `prettyprinter` library. Users often confuse the two. `prettyprinter` offers enhanced features like syntax highlighting, a more flexible API, and extensibility, which the standard library module does not.
fix
Always explicitly `from prettyprinter import pprint` to ensure you are using the external library. Avoid `import pprint` if you intend to use `prettyprinter`'s features, as it will import the standard library module.
affects: All versions
gotchaFor `prettyprinter` to automatically format output in Python shells (like IPython or the default Python REPL) or for it to pretty-print types from other popular libraries (e.g., `dataclasses`, `attrs`, `django`), you must explicitly call `install_extras()`. Without this, only basic types will be pretty-printed, and shell integration will not occur.
fix
Call `from prettyprinter import install_extras; install_extras(['python'])` for the default shell or `install_extras(['ipython'])` for IPython. You can also specify other libraries like `install_extras(include=['dataclasses', 'requests'])`. For persistent shell integration, consider setting the `PYTHONSTARTUP` environment variable.
affects: All versions
deprecatedWhen defining custom pretty printers, the `PrettyContext.set` method was deprecated in favor of `PrettyContext.assoc`. While `set` might still work in older versions, `assoc` is the recommended and clearer method for modifying context values.
fix
Update your custom pretty printer definitions to use `ctx.assoc(...)` instead of `ctx.set(...)` for context manipulation.
affects: 0.8.0 and later
gotchaWhen trying to pretty-print custom objects, simply defining a `__repr__` method on your class might not fully leverage `prettyprinter`'s capabilities or might even be overridden by `prettyprinter`'s default handling for certain base types. The library's declarative approach provides a more robust way.
fix
For optimal and consistent pretty-printing of custom types, use `prettyprinter.register_pretty` with a dedicated pretty printer function, combined with `pretty_call`, rather than solely relying on `__repr__`.
affects: All versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'prettyprinter'
The 'prettyprinter' library has not been installed in the current Python environment.
fix
Install the package using pip: `pip install prettyprinter`
NameError: name 'pprint' is not defined
The 'pprint' function from the 'prettyprinter' library was used without being explicitly imported or qualified.
fix
Import the 'pprint' function: `from prettyprinter import pprint` or use `import prettyprinter` and then `prettyprinter.pprint(...)`
AttributeError: module 'pprint' has no attribute 'install_extras'
The user attempted to call `install_extras` on Python's standard library `pprint` module, mistaking it for the `prettyprinter` library. `install_extras` is a function specific to the `prettyprinter` library.
fix
Ensure you are importing `install_extras` from the `prettyprinter` library: `from prettyprinter import install_extras`
AttributeError: module 'pprint' has no attribute 'register_pretty'
The user attempted to use the `@register_pretty` decorator (or function) from the standard library `pprint` module, mistaking it for the `prettyprinter` library. `register_pretty` is a feature of the `prettyprinter` library for defining custom pretty printers.
fix
Ensure you are importing `register_pretty` from the `prettyprinter` library: `from prettyprinter import register_pretty`
Upgrade
Version history
0.18.0latest on PyPI · released Jun 22, 2019
Audit
Dependencies

No dependency data recorded yet.

Agent activity
21 hits · last 30 days
node
18
OpenAI (training)
2
Resources
prettyprinter — pip install prettyprinter · libregistry