如何确定终端是否支持ANSI与python?

时间:2015-11-20 09:18:12

标签: python eclipse console window

问题在标题中:)

更准确地说,我使用colorama从我的脚本中获得彩色输出:

from colorama import init, Fore

init() # comment if running from Eclipse
print(Fore.RED + 'some red text')

我主要使用我的脚本和2终端:

  • Eclipse控制台,更准确地说是ANSI控制台插件,它支持ANSI控制序列

  • Windows控制台,可以输出颜色,但不能输出ANSI控制序列。

通常,colorama.init()确定是否使用Windows并修改ANSI序列,以便正确显示颜色。

但是在Eclipse中执行脚本时不需要这种行为。事实上,颜色将不会显示,因为colorama.init()可以替换ANSI序列(毕竟我们在Windows上),但Eclipse控制台不理解Windows序列。

如何从我的脚本中检测到我正在运行Eclipse或Windows控制台?

通常,我想这样做:

from colorama import init, Fore

if windows_console():
    init()
print(Fore.RED + 'some red text')

1 个答案:

答案 0 :(得分:0)

我很抱歉,但我的问题实际上是How to tell if python script is being run in a terminal or via GUI?

的重新制定

os.isatty(0)似乎有用。

相关问题