简体   繁体   中英

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: 在此输入图像描述

However, when I run code from the terminal (because ipython cannot do everything yet), I don't get any color, like so: 在此输入图像描述

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM