{"id":5376,"library":"prettyprinter","title":"PrettyPrinter","description":"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.","status":"active","version":"0.18.0","language":"en","source_language":"en","source_url":"https://github.com/tommikaikkonen/prettyprinter","tags":["pretty-printing","debugging","console","formatting","syntax-highlighting","developer-tool"],"install":[{"cmd":"pip install prettyprinter","lang":"bash","label":"Install stable version"}],"dependencies":[],"imports":[{"note":"To use prettyprinter's main function, explicitly import `pprint` from `prettyprinter` to avoid conflict with the standard library's `pprint` module.","wrong":"import pprint # This imports the standard library module","symbol":"pprint","correct":"from prettyprinter import pprint"},{"note":"Used for enabling integrations with shells (like IPython) or other third-party libraries (e.g., Django, NumPy, Requests).","symbol":"install_extras","correct":"from prettyprinter import install_extras"},{"note":"These functions are used when defining custom pretty-printing rules for your own Python types.","symbol":"register_pretty","correct":"from prettyprinter import register_pretty, pretty_call"}],"quickstart":{"code":"from prettyprinter import pprint, register_pretty, pretty_call\n\n# Basic pretty-printing of a nested dictionary\ndata = {\n    'name': 'PrettyPrinter Example',\n    'details': {\n        'id': 12345,\n        'items': [\n            {'item_id': 'A1', 'value': 100.5, 'tags': ['new', 'sale']},\n            {'item_id': 'B2', 'value': 200, 'tags': ['old']},\n            {'item_id': 'C3', 'value': 50.75, 'tags': []}\n        ],\n        'status': 'active'\n    },\n    'configs': [\n        {'key': 'timeout', 'value': 30},\n        {'key': 'retries', 'value': 5}\n    ]\n}\n\nprint('--- Standard print ---')\nprint(data)\nprint('\\n--- PrettyPrinter output ---')\npprint(data)\n\n# Custom pretty-printing for a user-defined class\nclass MyObject:\n    def __init__(self, name, values):\n        self.name = name\n        self.values = values\n\n@register_pretty(MyObject)\ndef pretty_my_object(value, ctx):\n    return pretty_call(\n        ctx,\n        MyObject,\n        name=value.name,\n        values=value.values\n    )\n\nmy_instance = MyObject('CustomItem', [{'x': 1, 'y': 2}, {'z': 3}])\nprint('\\n--- Custom object pretty-printing ---')\npprint(my_instance)\n\n# To enable shell integration (e.g., for `ipython` or default python shell):\n# from prettyprinter import install_extras\n# install_extras(['ipython']) # or ['python']\n# {'a': 1, 'b': 2} # will now be pretty-printed in the shell","lang":"python","description":"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."},"warnings":[{"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`.","message":"The 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.","severity":"breaking","affected_versions":"0.8.0 and later"},{"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.","message":"There 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.","severity":"gotcha","affected_versions":"All versions"},{"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.","message":"For `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.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Update your custom pretty printer definitions to use `ctx.assoc(...)` instead of `ctx.set(...)` for context manipulation.","message":"When 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.","severity":"deprecated","affected_versions":"0.8.0 and later"},{"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__`.","message":"When 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.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-13T00:00:00.000Z","next_check":"2026-07-12T00:00:00.000Z"}