Windows cmd:管道python 3.5 py文件结果有效,但pyinstaller exe会导致UnicodeEncodeError



我在这里有些不可用...

# -*- coding: utf-8 -*-
print(chr(246) + " " + chr(9786) + " " + chr(9787))
print("End.")

当我在Win7 CMD窗口中运行上面提到的代码时,我会根据我调用的方式得到结果:

python.exe utf8.py
-> ö ☺ ☻
python.exe utf8.py >test.txt
-> ö ☺ ☻ (in file)
utf8.exe
-> ö ☺ ☻
utf8.exe >test.txt
RuntimeWarning: sys.stdin.encoding == 'utf-8', whereas sys.stdout.encoding == 'cp1252', readline hook consumer may assume they are the same
Traceback (most recent call last):
  File "Developmentutf8.py", line 15, in <module>
    print(chr(246) + " " + chr(9786) + " " + chr(9787))
  File "C:python35libencodingscp1252.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode character 'u263a' in position 

与win_unicode_console在一起也无济于事。最后,我得到了相同的结果。

PYTHONIOENCODING=utf-8

已设置。但是看来,当使用pyinstaller时,该参数被忽略用于stdout.coding:

print(sys.stdout.encoding)
print(sys.stdout.isatty())
print(locale.getpreferredencoding())
print(sys.getfilesystemencoding())
print(os.environ["PYTHONIOENCODING"])

输出:

python.exe utf8.py > test.txt
utf-8
False
cp1252
mbcs
utf-8
utf8.exe >test.txt
cp1252
False
cp1252
mbcs
utf-8

问题是:如何发生?和:我该如何解决?

codecs.getwriter([something])(sys.stdout)

似乎不鼓励,因为它可能导致输出损坏的模块。还是可以强制使用UTF-8,以防我们进行TTY检查?更好:如何在Pyinstaller中解决该问题?

预先感谢...

感谢Eryksun,以下解决方法正在工作:

STDOUT_ENCODING = str(sys.stdout.encoding)
try:
    PYTHONIOENCODING = str(os.environ["PYTHONIOENCODING"])
except:
    PYTHONIOENCODING = False
# Remark: In case the stdout gets modified, it will only append all information
# that has been written into the pipe until that very moment.
if sys.stdout.isatty() is False:
    print("Program is running in piping mode. (sys.stdout.isatty() is " + str(sys.stdout.isatty()) + ".)")
    if PYTHONIOENCODING is not False:
        print("PYTHONIOENCODING is set to a value. ('" + str(PYTHONIOENCODING) + "')")
        if str(sys.stdout.encoding) != str(PYTHONIOENCODING):
            print("PYTHONIOENCODING is differing from stdout encoding. ('" + str(PYTHONIOENCODING) + "' != '" + STDOUT_ENCODING + "'). This should normally not happen unless the PyInstaller setup is still broken. Setting hard utf-8 workaround.")
            sys.stdout = open(sys.stdout.fileno(), 'w', encoding='utf-8', closefd=False)
            print("PYTHONIOENCODING was differing from stdout encoding. ('" + str(PYTHONIOENCODING) + "' != '" + STDOUT_ENCODING + "'). This should normally not happen unless PyInstaller is still broken. Setting hard utf-8 workaround. New encoding: '" + str(PYTHONIOENCODING) + "'.", "D")
        else:
            print("PYTHONIOENCODING is equal to stdout encoding. ('" + str(PYTHONIOENCODING) + "' == '" + str(sys.stdout.encoding) + "'). - All good.")
    else:
        print("PYTHONIOENCODING is set False. ('" + str(PYTHONIOENCODING) + "'). - Nothing to do.")
else:
    print("Program is running in terminal mode. (sys.stdout.isatty() is " + str(sys.stdout.isatty()) + ".) - All good.")

试图设置一个新的pyinstaller-envrientment,以查看从接下来的开始是否修复它。

最新更新