Skip to content

Instantly share code, notes, and snippets.

@EdwardBetts
Last active March 19, 2024 18:17
Show Gist options
  • Star 46 You must be signed in to star a gist
  • Fork 11 You must be signed in to fork a gist
  • Save EdwardBetts/0814484fdf7bbf808f6f to your computer and use it in GitHub Desktop.
Save EdwardBetts/0814484fdf7bbf808f6f to your computer and use it in GitHub Desktop.
Python pprint with color syntax highlighting for the console
from pprint import pformat
from typing import Any
from pygments import highlight
from pygments.formatters import Terminal256Formatter
from pygments.lexers import PythonLexer
def pprint_color(obj: Any) -> None:
"""Pretty-print in color."""
print(highlight(pformat(obj), PythonLexer(), Terminal256Formatter()), end="")
@jasonk
Copy link

jasonk commented Aug 21, 2022

It doesn't work in newer versions because python no longer allows you to call print without parentheses. Just add parentheses to the last line and it will work:

    print( highlight(pformat(obj), PythonLexer(), Terminal256Formatter()) )

@EdwardBetts
Copy link
Author

I've updated the gist for Python 3 and added type hints.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment