首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Windows cmd: piping python3.5py文件结果工作,但pyinstaller exe的指向UnicodeEncodeError

Windows cmd: piping python3.5py文件结果工作,但pyinstaller exe的指向UnicodeEncodeError
EN

Stack Overflow用户
提问于 2017-06-27 20:36:15
回答 1查看 385关注 0票数 1

我在这里有点别无选择...

代码语言:javascript
复制
# -*- coding: utf-8 -*-
print(chr(246) + " " + chr(9786) + " " + chr(9787))
print("End.")

当我在我的Win7命令窗口中运行上面提到的代码时,我得到的结果取决于我调用它的方式:

代码语言:javascript
复制
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 "Development\utf8.py", line 15, in <module>
    print(chr(246) + " " + chr(9786) + " " + chr(9787))
  File "C:\python35\lib\encodings\cp1252.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也于事无补。最后,我得到了同样的结果。

代码语言:javascript
复制
PYTHONIOENCODING=utf-8

已设置。但是,似乎在使用PyInstaller时,对于stdout.encoding,该参数被忽略:

代码语言:javascript
复制
print(sys.stdout.encoding)
print(sys.stdout.isatty())
print(locale.getpreferredencoding())
print(sys.getfilesystemencoding())
print(os.environ["PYTHONIOENCODING"])

输出:

代码语言:javascript
复制
python.exe utf8.py > test.txt
utf-8
False
cp1252
mbcs
utf-8

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

问题是:这是如何发生的?还有:我该如何解决这个问题呢?

代码语言:javascript
复制
codecs.getwriter([something])(sys.stdout)

似乎是不鼓励的,因为它可能会导致模块的输出中断。或者有没有可能强制将其转换为utf-8,以防我们对tty进行检查?更好的是:如何在PyInstaller中修复这个问题?

先谢谢你...

EN

回答 1

Stack Overflow用户

发布于 2017-06-29 16:38:14

多亏了eryksun,以下解决方法正在运行:

代码语言:javascript
复制
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-Environment,看看是否可以从头开始修复它。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44780476

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档