2015-11-13 164 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更新後,該變量被刪除,所以我也刪除了我的解決方法。