如何让python的终端错误变成彩色?

时间:2016-05-06 20:44:21

标签: python linux terminal

我一直在使用iPython笔记本电脑一段时间,我真的很感激错误输出(如果我发出拼写/语法错误)的颜色是这样的: enter image description here

但是,当我从终端运行代码时(因为ipython还不能完成所有操作),我不会得到任何颜色,如下所示: enter image description here

当然这可能因终端/操作系统而异,但我很好奇是否有任何简单的包/插件可以让终端中的python错误输出为彩色?甚至要寻找什么(我在ubuntu上运行zsh)。

1 个答案:

答案 0 :(得分:5)

通过IPython API引用开始IPython.core.ultratb,模块IPython本身用于丰富多彩的异常格式化。你应该可以做到

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

检查IPython是否可用,如果可用,请使用其例外打印机。