Windows cmd 编码更改导致 Python 崩溃

2025-02-20 09:23:00
admin
原创
24
摘要:问题描述:首先,我将 Windows CMD 编码更改为 utf-8 并运行 Python 解释器:chcp 65001 python 然后我尝试在其中打印一个unicode字符串,当我这样做时,Python 以一种特殊的方式崩溃(我只是在同一个窗口中得到一个 cmd 提示符)。>>> im...

问题描述:

首先,我将 Windows CMD 编码更改为 utf-8 并运行 Python 解释器:

chcp 65001
python

然后我尝试在其中打印一个unicode字符串,当我这样做时,Python 以一种特殊的方式崩溃(我只是在同一个窗口中得到一个 cmd 提示符)。

>>> import sys
>>> print u'ëèæîð'.encode(sys.stdin.encoding)

知道为什么会发生这种情况以及如何使其发挥作用吗?

UPDsys.stdin.encoding退货'cp65001'

UPD2:我刚刚意识到这个问题可能与 utf-8 使用多字节字符集有关(kcwu 对此提出了一个很好的观点)。我尝试使用“windows-1250”运行整个示例,结果得到了“ëeaî?”。Windows-1250 使用单字符集,因此它适用于它能理解的那些字符。但是我仍然不知道如何让“utf-8”在这里发挥作用。

UPD3:哦,我发现这是一个已知的 Python 错误。我猜想发生的事情是 Python 将 cmd 编码作为 'cp65001 复制到 sys.stdin.encoding 并尝试将其应用于所有输入。由于它无法理解 'cp65001',因此它会在任何包含非 ASCII 字符的输入上崩溃。


解决方案 1:

更新:在 Python 3.6 或更高版本中,将 Unicode 字符串打印到 Windows 上的控制台即可。

在 Python 3.8 或更高版本中,这个问题中描述的底层错误已通过将 cp65001 设为 utf-8 的别名而得到修复,正如 Boris Verkhovskiy 的回答所指出的那样。

因此,从本质上讲,升级到最新的 Python 就大功告成了。此时,我建议根据需要使用将代码更新到 Python 3.x,并放弃对 Python 2.x 的支持。请注意,自2021 年 12 月2to3以来,3.7 之前的任何 Python 版本(包括 Python 2.7)均不提供安全支持。

如果您确实仍然需要支持早期版本的 Python(包括 Python 2.7),您可以使用https://github.com/Drekin/win-unicode-console,它最初基于此答案中的使用代码WriteConsoleW


上一个答案

以下是如何cp65001在不进行更改的情况下将别名设置为 UTF-8 encodingsaliases.py

import codecs
codecs.register(lambda name: codecs.lookup('utf-8') if name == 'cp65001' else None)

(恕我直言,不要理会http://bugs.python.org/issue6058#msg97731cp65001上关于与 UTF-8 不相同的愚蠢言论。即使微软的编解码器有一些小错误,它也应该是相同的。)

以下是一些代码(为 Tahoe-LAFS、tahoe-lafs.org 编写),它使控制台输出无论代码页如何都可以工作chcp,并且还可以读取 Unicode 命令行参数。感谢Michael Kaplan提出此解决方案的想法。如果 stdout 或 stderr 被重定向,它将输出 UTF-8。如果您想要字节顺序标记,则需要明确编写它。

[编辑:此版本使用了MSVC 运行库中的标志,但该标志存在缺陷。WriteConsoleW相对于 MS 文档而言,该版本也存在缺陷,但缺陷较少。]_O_U8TEXT`WriteConsoleW`

import sys
if sys.platform == "win32":
    import codecs
    from ctypes import WINFUNCTYPE, windll, POINTER, byref, c_int
    from ctypes.wintypes import BOOL, HANDLE, DWORD, LPWSTR, LPCWSTR, LPVOID

    original_stderr = sys.stderr

    # If any exception occurs in this code, we'll probably try to print it on stderr,
    # which makes for frustrating debugging if stderr is directed to our wrapper.
    # So be paranoid about catching errors and reporting them to original_stderr,
    # so that we can at least see them.
    def _complain(message):
        print >>original_stderr, message if isinstance(message, str) else repr(message)

    # Work around <http://bugs.python.org/issue6058>.
    codecs.register(lambda name: codecs.lookup('utf-8') if name == 'cp65001' else None)

    # Make Unicode console output work independently of the current code page.
    # This also fixes <http://bugs.python.org/issue1602>.
    # Credit to Michael Kaplan <http://www.siao2.com/2010/04/07/9989346.aspx>
    # and TZOmegaTZIOY
    # <http://stackoverflow.com/questions/878972/windows-cmd-encoding-change-causes-python-crash/1432462#1432462>.
    try:
        # <http://msdn.microsoft.com/en-us/library/ms683231(VS.85).aspx>
        # HANDLE WINAPI GetStdHandle(DWORD nStdHandle);
        # returns INVALID_HANDLE_VALUE, NULL, or a valid handle
        #
        # <http://msdn.microsoft.com/en-us/library/aa364960(VS.85).aspx>
        # DWORD WINAPI GetFileType(DWORD hFile);
        #
        # <http://msdn.microsoft.com/en-us/library/ms683167(VS.85).aspx>
        # BOOL WINAPI GetConsoleMode(HANDLE hConsole, LPDWORD lpMode);

        GetStdHandle = WINFUNCTYPE(HANDLE, DWORD)(("GetStdHandle", windll.kernel32))
        STD_OUTPUT_HANDLE = DWORD(-11)
        STD_ERROR_HANDLE = DWORD(-12)
        GetFileType = WINFUNCTYPE(DWORD, DWORD)(("GetFileType", windll.kernel32))
        FILE_TYPE_CHAR = 0x0002
        FILE_TYPE_REMOTE = 0x8000
        GetConsoleMode = WINFUNCTYPE(BOOL, HANDLE, POINTER(DWORD))(("GetConsoleMode", windll.kernel32))
        INVALID_HANDLE_VALUE = DWORD(-1).value

        def not_a_console(handle):
            if handle == INVALID_HANDLE_VALUE or handle is None:
                return True
            return ((GetFileType(handle) & ~FILE_TYPE_REMOTE) != FILE_TYPE_CHAR
                    or GetConsoleMode(handle, byref(DWORD())) == 0)

        old_stdout_fileno = None
        old_stderr_fileno = None
        if hasattr(sys.stdout, 'fileno'):
            old_stdout_fileno = sys.stdout.fileno()
        if hasattr(sys.stderr, 'fileno'):
            old_stderr_fileno = sys.stderr.fileno()

        STDOUT_FILENO = 1
        STDERR_FILENO = 2
        real_stdout = (old_stdout_fileno == STDOUT_FILENO)
        real_stderr = (old_stderr_fileno == STDERR_FILENO)

        if real_stdout:
            hStdout = GetStdHandle(STD_OUTPUT_HANDLE)
            if not_a_console(hStdout):
                real_stdout = False

        if real_stderr:
            hStderr = GetStdHandle(STD_ERROR_HANDLE)
            if not_a_console(hStderr):
                real_stderr = False

        if real_stdout or real_stderr:
            # BOOL WINAPI WriteConsoleW(HANDLE hOutput, LPWSTR lpBuffer, DWORD nChars,
            #                           LPDWORD lpCharsWritten, LPVOID lpReserved);

            WriteConsoleW = WINFUNCTYPE(BOOL, HANDLE, LPWSTR, DWORD, POINTER(DWORD), LPVOID)(("WriteConsoleW", windll.kernel32))

            class UnicodeOutput:
                def __init__(self, hConsole, stream, fileno, name):
                    self._hConsole = hConsole
                    self._stream = stream
                    self._fileno = fileno
                    self.closed = False
                    self.softspace = False
                    self.mode = 'w'
                    self.encoding = 'utf-8'
                    self.name = name
                    self.flush()

                def isatty(self):
                    return False

                def close(self):
                    # don't really close the handle, that would only cause problems
                    self.closed = True

                def fileno(self):
                    return self._fileno

                def flush(self):
                    if self._hConsole is None:
                        try:
                            self._stream.flush()
                        except Exception as e:
                            _complain("%s.flush: %r from %r" % (self.name, e, self._stream))
                            raise

                def write(self, text):
                    try:
                        if self._hConsole is None:
                            if isinstance(text, unicode):
                                text = text.encode('utf-8')
                            self._stream.write(text)
                        else:
                            if not isinstance(text, unicode):
                                text = str(text).decode('utf-8')
                            remaining = len(text)
                            while remaining:
                                n = DWORD(0)
                                # There is a shorter-than-documented limitation on the
                                # length of the string passed to WriteConsoleW (see
                                # <http://tahoe-lafs.org/trac/tahoe-lafs/ticket/1232>.
                                retval = WriteConsoleW(self._hConsole, text, min(remaining, 10000), byref(n), None)
                                if retval == 0 or n.value == 0:
                                    raise IOError("WriteConsoleW returned %r, n.value = %r" % (retval, n.value))
                                remaining -= n.value
                                if not remaining:
                                    break
                                text = text[n.value:]
                    except Exception as e:
                        _complain("%s.write: %r" % (self.name, e))
                        raise

                def writelines(self, lines):
                    try:
                        for line in lines:
                            self.write(line)
                    except Exception as e:
                        _complain("%s.writelines: %r" % (self.name, e))
                        raise

            if real_stdout:
                sys.stdout = UnicodeOutput(hStdout, None, STDOUT_FILENO, '<Unicode console stdout>')
            else:
                sys.stdout = UnicodeOutput(None, sys.stdout, old_stdout_fileno, '<Unicode redirected stdout>')

            if real_stderr:
                sys.stderr = UnicodeOutput(hStderr, None, STDERR_FILENO, '<Unicode console stderr>')
            else:
                sys.stderr = UnicodeOutput(None, sys.stderr, old_stderr_fileno, '<Unicode redirected stderr>')
    except Exception as e:
        _complain("exception %r while fixing up sys.stdout and sys.stderr" % (e,))


    # While we're at it, let's unmangle the command-line arguments:

    # This works around <http://bugs.python.org/issue2128>.
    GetCommandLineW = WINFUNCTYPE(LPWSTR)(("GetCommandLineW", windll.kernel32))
    CommandLineToArgvW = WINFUNCTYPE(POINTER(LPWSTR), LPCWSTR, POINTER(c_int))(("CommandLineToArgvW", windll.shell32))

    argc = c_int(0)
    argv_unicode = CommandLineToArgvW(GetCommandLineW(), byref(argc))

    argv = [argv_unicode[i].encode('utf-8') for i in xrange(0, argc.value)]

    if not hasattr(sys, 'frozen'):
        # If this is an executable produced by py2exe or bbfreeze, then it will
        # have been invoked directly. Otherwise, unicode_argv[0] is the Python
        # interpreter, so skip that.
        argv = argv[1:]

        # Also skip option arguments to the Python interpreter.
        while len(argv) > 0:
            arg = argv[0]
            if not arg.startswith(u"-") or arg == u"-":
                break
            argv = argv[1:]
            if arg == u'-m':
                # sys.argv[0] should really be the absolute path of the module source,
                # but never mind
                break
            if arg == u'-c':
                argv[0] = u'-c'
                break

    # if you like:
    sys.argv = argv

最后,可以满足ΤΖΩΤΖΙΟΥ 的愿望,在控制台上使用 DejaVu Sans Mono,我同意这是一种很棒的字体。

您可以在“命令窗口中可用字体的必要条件”Microsoft KB中找到有关字体要求以及如何为 Windows 控制台添加新字体的信息

但基本上,在 Vista 上(可能还有 Win7):

  • 在 下HKEY_LOCAL_MACHINE_SOFTWAREMicrosoftWindows NTCurrentVersionConsoleTrueTypeFont,设置"0""DejaVu Sans Mono"

  • 对于 下的每个子项HKEY_CURRENT_USERConsole,设置"FaceName""DejaVu Sans Mono"

在 XP 上,检查 LockerGnome 论坛中的主题“更改命令提示符字体?”。

解决方案 2:

设置PYTHONIOENCODING系统变量:

> chcp 65001
> set PYTHONIOENCODING=utf-8
> python example.py
Encoding is utf-8

来源example.py很简单:

import sys
print "Encoding is", sys.stdin.encoding

解决方案 3:

对于我来说,在执行 python 程序之前设置这个环境变量是有效的:

set PYTHONIOENCODING=utf-8

解决方案 4:

你想让 Python 编码为 UTF-8 吗?

>>>print u'ëèæîð'.encode('utf-8')
ëèæîð

Python 不会将 cp65001 识别为 UTF-8。

解决方案 5:

我也遇到过这个烦人的问题,我讨厌无法在 MS Windows 和 Linux 中运行支持 unicode 的脚本。因此,我设法想出了一个解决方法。

采取这个脚本(例如,uniconsole.py在您的站点包或其他地方):

import sys, os

if sys.platform == "win32":
    class UniStream(object):
        __slots__= ("fileno", "softspace",)

        def __init__(self, fileobject):
            self.fileno = fileobject.fileno()
            self.softspace = False

        def write(self, text):
            os.write(self.fileno, text.encode("utf_8") if isinstance(text, unicode) else text)

    sys.stdout = UniStream(sys.stdout)
    sys.stderr = UniStream(sys.stderr)

这似乎可以解决 python 错误(或 win32 unicode 控制台错误,无论如何)。然后我添加了所有相关脚本:

try:
    import uniconsole
except ImportError:
    sys.exc_clear()  # could be just pass, of course
else:
    del uniconsole  # reduce pollution, not needed anymore

chcp 65001最后,我只需在运行 且字体为 的控制台中根据需要运行脚本即可Lucida Console。(我多么希望DejaVu Sans Mono可以改用它……但破解注册表并将其选为控制台字体会恢复为位图字体。)

这是一个快速而粗糙stdoutstderr替换,并且不处理任何raw_input相关错误(显然,因为它根本没有触及sys.stdin)。顺便说一句,我在标准库文件中添加了cp65001别名。utf_8`encodingsaliases.py`

解决方案 6:

对于未知编码:cp65001 问题,可以将新变量设置为 PYTHONIOENCODING,并将设置为 UTF-8。(这对我有用)

查看此内容:

查看此内容

解决方案 7:

该问题已得到解决,并在此线程中处理:

更改系统编码

解决方案是取消选择 Unicode UTF-8 以获得 Win 中的全球支持。这将需要重新启动,然后你的 Python 应该会恢复正常。

获胜步骤:

  1. 进入控制面板

  2. 选择时钟和区域

  3. 单击区域 > 行政

  4. 在非 Unicode 程序的语言中单击“更改系统区域设置”

  5. 在弹出的“区域设置”窗口中取消勾选“Beta:使用 Unicode UTF-8...”

  6. 根据 Win 提示重新启动机器

图片显示了如何解决问题的确切位置:

如何解决这个问题

解决方案 8:

从 Python 3.8+ 开始,编码cp65001utf-8

https://docs.python.org/library/codecs.html#standard-encodings

解决方案 9:

这是因为 cmd 的“代码页”与系统的“mbcs”不同。尽管你改变了“代码页”,但 python(实际上是 windows)仍然认为你的“mbcs”没有改变。

解决方案 10:

一些评论:你可能拼错了encodig.code。这是我对你的示例的运行。

C:>chcp 65001
Active code page: 65001

C:>python25python
...
>>> import sys
>>> sys.stdin.encoding
'cp65001'
>>> s=u'/u0065/u0066'
>>> s
u'ef'
>>> s.encode(sys.stdin.encoding)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
LookupError: unknown encoding: cp65001
>>>

结论 -cp65001不是 Python 的已知编码。请尝试“UTF-16”或类似编码。

相关推荐
  为什么项目管理通常仍然耗时且低效?您是否还在反复更新电子表格、淹没在便利贴中并参加每周更新会议?这确实是耗费时间和精力。借助软件工具的帮助,您可以一目了然地全面了解您的项目。如今,国内外有足够多优秀的项目管理软件可以帮助您掌控每个项目。什么是项目管理软件?项目管理软件是广泛行业用于项目规划、资源分配和调度的软件。它使项...
项目管理软件   1325  
  IPD(Integrated Product Development)流程作为一种先进的产品开发管理模式,在众多企业中得到了广泛应用。它涵盖了从产品概念产生到产品退市的整个生命周期,通过整合跨部门团队、优化流程等方式,显著提升产品开发的效率和质量,进而为项目的成功奠定坚实基础。深入探究IPD流程的五个阶段与项目成功之间...
IPD流程分为几个阶段   4  
  华为作为全球知名的科技企业,其成功背后的管理体系备受关注。IPD(集成产品开发)流程作为华为核心的产品开发管理模式,其中的创新管理与实践更是蕴含着丰富的经验和深刻的智慧,对众多企业具有重要的借鉴意义。IPD流程的核心架构IPD流程旨在打破部门墙,实现跨部门的高效协作,将产品开发视为一个整体的流程。它涵盖了从市场需求分析...
华为IPD是什么   3  
  IPD(Integrated Product Development)研发管理体系作为一种先进的产品开发模式,在众多企业的发展历程中发挥了至关重要的作用。它不仅仅是一套流程,更是一种理念,一种能够全方位提升企业竞争力,推动企业持续发展的有效工具。深入探究IPD研发管理体系如何助力企业持续发展,对于众多渴望在市场中立足并...
IPD管理流程   3  
  IPD(Integrated Product Development)流程管理旨在通过整合产品开发流程、团队和资源,实现产品的快速、高质量交付。在这一过程中,有效降低成本是企业提升竞争力的关键。通过优化IPD流程管理中的各个环节,可以在不牺牲产品质量和性能的前提下,实现成本的显著降低,为企业创造更大的价值。优化产品规划...
IPD流程分为几个阶段   4  
热门文章
项目管理软件有哪些?
云禅道AD
禅道项目管理软件

云端的项目管理软件

尊享禅道项目软件收费版功能

无需维护,随时随地协同办公

内置subversion和git源码管理

每天备份,随时转为私有部署

免费试用