gpt4 book ai didi

python - 如何在 Git Bash/WinPTY 下使用 Python3 获得 REPL/箭头键功能和 Unicode 字符

转载 作者:太空宇宙 更新时间:2023-11-03 14:25:55 27 4
gpt4 key购买 nike

Windows 7,受限用户帐户运行Git git 版本 2.14.1.windows.1

MINGW/MSYS MINGW64_NT-6.1 已编辑 2.8.2(0.313/5/3) 2017-07-12 15:35 x86_64 Msys

Python Win32 上的 Python 3.4.3(v3.4.3:9b73f1c3e601,2015 年 2 月 24 日,22:44:40)[MSC v.1600 64 位 (AMD64)]

区域设置已设置:

$ locale
LANG=en_US.UTF-8
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_ALL=

Python IO 编码环境变量已设置

$ env | grep "PYTHON"
PYTHONIOENCODING=utf-8

现在进行“测试”:

$ /c/Python34/python.exe -i
Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:44:40) [MSC v.1600 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.stdout.encoding
'utf-8'
>>> print('\u2660')

>>>

Unicode 可以工作,但箭头键功能在 REPL 中已丢失。因此,请使用 WinPTY 执行

$ winpty /c/Python34/python.exe -i
Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:44:40) [MSC v.1600 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.stdout.encoding
'utf-8'
>>> print('\u2660')
ΓÖá
>>>

现在我可以在 REPL 中浏览历史记录,但 Unicode 不太可靠。

所以看起来我可以使用 REPL 中的箭头键或正确的 unicode 编码,但不能同时使用两者。其他人也有这个问题吗?还是我错过了一些可以让这一切顺利进行的东西?

注意:我仅限于此环境;我无法随意安装/更新/修改应用程序。

最佳答案

在 WinPTY 情况下,控制台仍然使用 cp437(默认的美国 OEM Windows 编码),但您已强制 Python 通过 PYTHONIOENCODING 输出 UTF-8。切换到 Python 3.6,因为该版本现在使用 Windows Unicode 控制台 API 来输出 Unicode,并且不应出现编码问题。

关于python - 如何在 Git Bash/WinPTY 下使用 Python3 获得 REPL/箭头键功能和 Unicode 字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47637584/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com