Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get python's terminal error 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).

like image 696
dval Avatar asked May 06 '16 20:05

dval


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

like image 181
user2357112 supports Monica Avatar answered Oct 18 '22 17:10

user2357112 supports Monica