Skip to content

Instantly share code, notes, and snippets.

@chrisbraddock
Last active July 26, 2024 08:31
Show Gist options
  • Save chrisbraddock/6c58c744a46792f071579af9336038dc to your computer and use it in GitHub Desktop.
Save chrisbraddock/6c58c744a46792f071579af9336038dc to your computer and use it in GitHub Desktop.
colorize Python error output

colorize / syntax highlighting for Python console error output

pip install IPython

put early in your code (e.g. main.py)

# source: https://stackoverflow.com/a/52797444/227260

import sys
from IPython.core import ultratb

# the StackOverflow example showed this, but these values were defaults when I inspected.
# sys.excepthook = ultratb.FormattedTB(color_scheme='Linux', call_pdb=False)

# This works for me.
sys.excepthook = ultratb.FormattedTB()

# Verbose output
# sys.excepthook = ultratb.VerboseTB()

color_scheme

  • NoColor: allows all of this module to be used in any terminal (the color escapes are just dummy blank strings).
  • Linux: is meant to look good in a terminal like the Linux console (black or very dark background).
  • LightBG: similar to Linux but swaps dark/light colors to be more readable in light background terminals.
  • Neutral: a neutral color scheme that should be readable on both light and dark background

https://ipython.readthedocs.io/en/stable/api/generated/IPython.core.ultratb.html

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