newmathwhodis
newmathwhodis

Reputation: 3289

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

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).

Upvotes: 5

Views: 1365

Answers (1)

user2357112
user2357112

Reputation: 280778

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.

Upvotes: 10

Related Questions