2015-11-13 163 views
2

我将Python安装从3.4更新为3.5(Windows 7 Enterprise 64位上的CPython 64位)。在更新之后,colorama停止将ANSI转义序列转换为Win32 API调用以更改命令行终端颜色。Python 3.5:Colorama无法识别Windows环境

需要明确的colorama.init(convert=True)才能获得彩色输出。我试图缩小误差下来:

  1. 它出现了,因为Python的3.5更新
  2. 它可以围绕我是否隐含调用init()与转换选项来工作。
  3. 从cmd.exe启动一个彩色的Python脚本按预期工作。
  4. 从powershell.exe启动彩色的Python脚本显示描述的行为。

因此,我认为如果从Powershell启动,Python无法识别Windows环境?

任何人都可以重现这种奇怪的行为吗?我应该如何解决它。启用convert会给Linux带来问题。

回答

2

我搜索COLORAMA 0.3.3源和发现的代码确定它的运行窗口:

... 
on_windows = os.name == 'nt' 
on_emulated_windows = on_windows and 'TERM' in os.environ 

# should we strip ANSI sequences from our output? 
if strip is None: 
    strip = on_windows and not on_emulated_windows 
self.strip = strip 

# should we should convert ANSI sequences into win32 calls? 
if convert is None: 
    convert = on_windows and not wrapped.closed and not on_emulated_windows and is_a_tty(wrapped) 
self.convert = convert 
.... 

一种情况是,如果有一个TERM环境变量设置。不幸的是我的PowerShell控制台声称是一个cygwin终端。

但我从未安装过cygwin。所以我必须搜索哪个程序安装了cygwin并将其注册到我的PowerShell!中!

编辑:

我发现,即POSH-GIT中注册了TERM变量。作为一种解决方法,我在PoSh-Git加载后立即添加了一条rm env:TERM行。

PoSh-Git更新后,该变量被删除,所以我也删除了我的解决方法。