How to get python's terminal error messages to be in color?

Question:

I’ve been working with iPython notebooks for a while and I really appreciated how the error output (if I made a spelling/syntax error) was in color like this:
enter image description here

However, when I run code from the terminal (because iPython cannot do everything yet), I don’t get any color, like so:
enter image description here

Of course that might vary by terminal/operating system, but I was curious if there are any easy package/plugin to make Python error output in the terminal to be in color please? or even what to look for (I run zsh on Ubuntu).

Asked By: newmathwhodis

||

Answers:

Digging through the IPython API reference turns up IPython.core.ultratb, the module IPython itself uses for colorful exception formatting. You should be able to do

try:
    import IPython.core.ultratb
except ImportError:
    # No IPython. Use default exception printing.
    pass
else:
    import sys
    sys.excepthook = IPython.core.ultratb.ColorTB()

to check whether IPython is available, and if so, use its exception printer.

Answered By: user2357112
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.