【问题标题】:Error when using pyinstaller: UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff使用 pyinstaller 时出错:UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff
【发布时间】:2018-05-21 10:28:24
【问题描述】:

我在使用 pyinstaller 编译 PyQt 代码时遇到问题。

我用这一行编译:

c:\Anaconda3\Scripts\pyinstaller.exe -y -F --distpath="." MyQt.py

然后我收到此错误消息:

      File "c:\anaconda36bis\lib\site-packages\PyInstaller\hooks\hook-zmq.py", line
18, in <module>
    hiddenimports.extend(collect_submodules('zmq.backend'))
  File "c:\anaconda36bis\lib\site-packages\PyInstaller\utils\hooks\__init__.py",
 line 619, in collect_submodules
    repr(pkg_dir), package))
  File "c:\anaconda36bis\lib\site-packages\PyInstaller\utils\hooks\__init__.py",
 line 90, in exec_statement
    return __exec_python_cmd(cmd)
  File "c:\anaconda36bis\lib\site-packages\PyInstaller\utils\hooks\__init__.py",
 line 77, in __exec_python_cmd
    txt = exec_python(*cmd, env=pp_env)
  File "c:\anaconda36bis\lib\site-packages\PyInstaller\compat.py", line 562, in
exec_python
    return exec_command(*cmdargs, **kwargs)
  File "c:\anaconda36bis\lib\site-packages\PyInstaller\compat.py", line 369, in
exec_command
    out = out.decode(encoding)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 152: invali
d start byte

我不清楚错误消息,我不明白为什么会发生这种情况。

pyinstaller 是否有可能尝试使用不兼容的模块? 我在我的脚本中使用这些:

# -*- coding: utf-8 -*-
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
# from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import * 
import sys
import numpy as np
import matplotlib
matplotlib.use('Qt5Agg')
from matplotlib.figure import Figure
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as     FigureCanvas
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as     NavigationToolbar
from scipy.ndimage import imread
from scipy.ndimage.morphology import binary_dilation
from scipy.optimize import curve_fit, leastsq

更新

控制台打印的问题直接出现在

142859 INFO: Loading module hook "hook-zmq.py"...

那么应该是说这个错误是来自zmq的吧?

【问题讨论】:

    标签: python pyqt5 pyinstaller


    【解决方案1】:

    我在另一个论坛上找到了答案。 我更改了Python\Lib\site-packages\Pyinstaller\compat.py 文件中的第 369 行:

    out = out.decode(encoding)
    

    out = out.decode(encoding, errors='ignore')
    

    out = out.decode(encoding, "replace")
    

    现在我可以毫无问题地编译我的脚本了。 我仍然不知道为什么我的问题首先会发生,但至少现在可以编译了。

    【讨论】:

    • 非常感谢!这很简单,但它解决了一个大问题。我花了 2 天时间寻找解决方案。谢谢谢谢。而且很多人都是最近遇到的问题,和我们一样,但我不知道为什么。
    • 欢迎您。我想这将在下一个版本中得到纠正。
    • 我希望如此。你也有 PyQt 文件夹的问题吗?我必须将其所有内容复制到与自身相同的路径,因为在其他情况下,打开 GUI 会失败。
    • 不,我对 pyqt 没有问题
    • @decadenza 我昨天通过使用 out = out.decode(encoding, "replace") 而不是忽略来修复了我的问题。
    【解决方案2】:

    2 年后答案仍然有效,但线路从 368 变为 428。

    【讨论】:

      【解决方案3】:

      在最新版本 (3.5) 中,该行略微移动到 427。

      最好的办法是搜索

      out = out.decode(encoding)
      

      替换成

      out = out.decode(encoding, "replace")
      

      我不明白他们为什么不解决这个烦人的问题!

      【讨论】:

      • 谢谢,这在 Anaconda 中对我有用(在不同的计算机上已经两次)。
      【解决方案4】:

      更改 compat.py 对我有用: out = out.decode(encoding, "replace")

      这是 pyinstaller 上的一个已知问题,开发人员正在解决这个问题。 https://github.com/pyinstaller/pyinstaller/pull/3895

      我希望这个错误将在下一次更新中解决。

      【讨论】:

        【解决方案5】:

        自 4.0 版起该错误已修复

        pip install "pyinstaller>=4.0.0"
        

        【讨论】:

        • @赞道同在
        • 在 4.7 版中 - 不再工作((((通过添加“替换”修复)
        【解决方案6】:

        我在 MSYS2 下运行时遇到了这个问题。 为了解决这个问题,我将项目文件夹从 C:/Users/Χρήστος/Desktop 复制到 C:/Python/Scripts

        然后我从 MSYS2 终端运行 pyinstaller 命令,最后我在命令提示符控制台运行可执行文件。

        错误是在第一条路径中有“Χρήστος”(希腊名称),但在第二条路径中没有。

        注意:我在 msys64 目录中搜索文件 compat.py 但没有找到。

        【讨论】:

          猜你喜欢
          • 2021-11-24
          • 1970-01-01
          • 2020-12-26
          • 1970-01-01
          • 1970-01-01
          • 2013-11-11
          • 2022-01-13
          • 1970-01-01
          相关资源
          最近更新 更多