如何在Gnome终端中为Python错误的输出着色? [英] How to colorize the output of Python errors in the Gnome terminal?

查看:54
本文介绍了如何在Gnome终端中为Python错误的输出着色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注意:我在研究如何实际执行后问了这个问题.其他与我的问题有些相似但实际上与我不同的问题涉及:

Note: I am asking this question after researching how to actually do it. Other questions which are somewhat similar, but actually differ from my question relate to:

  1. python脚本中的颜色编码
  2. 在脚本内 导入颜色库
  3. 使用诸如 Solarized 之类的工具来改进诸如 Vim 之类的工具以对python代码进行颜色编码.
  1. Color-coding within the python script
  2. Importing color libraries within the script
  3. Using tools like Solarized to improve tools like Vim to color-code python code.

我在寻找什么我已经有一个简单的python脚本(例如test.py),并且正在Gnome终端上执行它.

What I am looking for: I already have a simple python script (say, test.py) and I am executing it from the Gnome terminal.

python test.py

test.py 将在终端中输出一些错误.错误示例:

test.py will output some errors right in the terminal. Error example:

Traceback (most recent call last):
  File "test.py", line 2, in <module>
    with open('', 'rb') as csvfile:
IOError: [Errno 2] No such file or directory: ''

我希望error关键字为粗体红色,例如,行号具有特定的颜色,以便易于查找错误.

I want the error keywords to be in bold red for example, line numbers in a specific color amongst others, such that it is easy to track down the errors.

如您所见,Stackoverflow已经很好地对错误进行了颜色编码.在我的Gnome终端上,输出是单色的.如何在终端上获得如此好的颜色编码输出?

As you can see, Stackoverflow already color-codes the error nicely. On my Gnome-terminal, the output is uni-color. How can I get such a nice color-coded output on the terminal?

谢谢!

推荐答案

您可以使用IPython着色.只需将其放在程序的开头即可.然后,每个异常将由ultratb处理,并以彩色显示,并显示导致异常的代码段的locals()值.

You could use the IPython coloring. Just put this at the beginning of your program. Every exception will be then handled by ultratb and be displayed in color and also show the locals() values for the code snippet that made the exception.

import sys
from IPython.core import ultratb
sys.excepthook = ultratb.FormattedTB(mode='Verbose', color_scheme='Linux', call_pdb=False)

即使您使用香草python解释器调用脚本,这也将起作用.

This will work even if you invoke your script with the vanilla python interpreter.

这篇关于如何在Gnome终端中为Python错误的输出着色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆