【问题标题】:cx_Freeze: ImportError: No module named 'PyQt5.Qt'cx_Freeze:ImportError:没有名为“PyQt5.Qt”的模块
【发布时间】:2019-08-02 20:30:41
【问题描述】:

我正在尝试使用 cx_Freeze 构建我的 GUI 应用程序。 setup.py如下:

from cx_Freeze import setup, Executable
import os
import sys

base = None

if sys.platform == 'win32':
    base = 'Win32GUI'

exe = [Executable("main.py", base=base, icon='window_icon_XbH_icon.ico')]

os.environ['TCL_LIBRARY'] = r'C:\\Users\\dm\\AppData\\Local\\Programs\\Python\\Python36\\tcl\\tcl8.6'
os.environ['TK_LIBRARY'] = r'C:\\Users\\dm\\AppData\\Local\\Programs\\Python\\Python36\\tcl\\tk8.6'

options = {
'build_exe': {
    'includes': ['scipy.io', 'scipy.spatial.ckdtree'],

    'include_files': [r'C:\\Users\\dm\\AppData\\Local\\Programs\\Python\\Python36\\DLLs\\tcl86t.dll', r'C:\\Users\\dm\\AppData\\Local\\Programs\\Python\\Python36\\DLLs\\tk86t.dll',
                      r'C:\\Users\\dm\\AppData\\Local\\Programs\\Python\\Python36\\Lib\\site-packages\\pyqt5_tools\\platforms\\qwindows.dll', 'window_icon_XbH_icon.ico'],
    'packages': ['pkg_resources._vendor', 'pandas', 'numpy', 'scipy', 'pydub', 'PyQt5', 'soundfile', 'sounddevice', 'cffi'],
}
}

setup(name="app", version="1.0", description='To be added',
  options=options, executables=exe)

在构建时,cx_Freeze 出现以下错误。我已经安装了 PyQt5。

running build
running build_exe
Traceback (most recent call last):
  File "setup.py", line 38, in <module>
options=options, executables=exe)
  File "C:\Users\dm\AppData\Local\Programs\Python\Python36\lib\site-packages\cx_Freeze\dist.py", line 349, in setup
distutils.core.setup(**attrs)
  File "C:\Users\dm\AppData\Local\Programs\Python\Python36\lib\distutils\core.py", line 148, in setup
dist.run_commands()
  File "C:\Users\dm\AppData\Local\Programs\Python\Python36\lib\distutils\dist.py", line 955, in run_commands
self.run_command(cmd)
  File "C:\Users\dm\AppData\Local\Programs\Python\Python36\lib\distutils\dist.py", line 974, in run_command
cmd_obj.run()
  File "C:\Users\dm\AppData\Local\Programs\Python\Python36\lib\distutils\command\build.py", line 135, in run
self.run_command(cmd_name)
  File "C:\Users\dm\AppData\Local\Programs\Python\Python36\lib\distutils\cmd.py", line 313, in run_command
self.distribution.run_command(command)
  File "C:\Users\dm\AppData\Local\Programs\Python\Python36\lib\distutils\dist.py", line 974, in run_command
cmd_obj.run()
  File "C:\Users\dm\AppData\Local\Programs\Python\Python36\lib\site-packages\cx_Freeze\dist.py", line 219, in run
freezer.Freeze()
  File "C:\Users\dm\AppData\Local\Programs\Python\Python36\lib\site-packages\cx_Freeze\freezer.py", line 616, in Freeze
self.finder = self._GetModuleFinder()
  File "C:\Users\dm\AppData\Local\Programs\Python\Python36\lib\site-packages\cx_Freeze\freezer.py", line 342, in _GetModuleFinder
finder.IncludePackage(name)
  File "C:\Users\dm\AppData\Local\Programs\Python\Python36\lib\site-packages\cx_Freeze\finder.py", line 659, in IncludePackage
module = self._ImportModule(name, deferredImports)
  File "C:\Users\dm\AppData\Local\Programs\Python\Python36\lib\site-packages\cx_Freeze\finder.py", line 351, in _ImportModule
raise ImportError("No module named %r" % name)
ImportError: No module named 'PyQt5.Qt'

谁能帮我弄清楚到底出了什么问题?

【问题讨论】:

  • 您使用的是哪个版本的 cx_Freeze?
  • @jpeg 最新的。 cx_Freeze 5.1.1

标签: python-3.x pyqt5 cx-freeze


【解决方案1】:

不要大写 PyQt5。在 setup.py 文件中尝试 pyqt5。

【讨论】:

    【解决方案2】:

    我知道这是一个老问题,但今天我遇到了这个问题。

    我就是这样解决的:

    从我的setup.py('{"build_exe": {"packages":' &lt;- this part). 中删除了 PyQt5 以及与之相关的所有内容 在它编译我的exe之后。 如果您运行 exe 并且存在缺少 PyQt5 模块错误,则从您的站点包目录 (path similar to this: d:\Python37\Lib\site-packages\PyQt5\) 将整个 PyQt5 目录复制到你的 cx_freeze 构建的 lib 目录(example: ..\build\exe.win-amd64-3.7\lib\)

    现在尝试运行exe,应该没有缺少模块的错误,至少与PyQt5有关。 如果您有任何其他缺少模块的问题,只需将其从您的站点包复制到 lib 目录中。希望对您有所帮助。

    【讨论】:

      【解决方案3】:

      尝试删除(不必要的?)行

      import PyQt5.Qt
      

      来自您的 setup.py 脚本。

      OP 删除此行后编辑:

      1. 尝试删除不必要的os.environ 语句,这些是针对tkinter 的,可能它们有冲突。删除 include_files 中的 3 个 DLL 条目(仅保留图标)。将'atexit' 添加到includes 列表中,参见cx_Freeze PyQt5 example

      2. 尝试重新安装PyQt5cx_Freeze,请参阅ImportError: No module named PytQt5PyQt5 and QtGui module not found 中的潜在警告。

      3. 如果这仍然不起作用,则可能与您的应用程序中使用的另一个包发生冲突。要找出答案,请仅使用PyQt5 制作一个最小示例,例如cx_Freeze PyQt5 example,然后尝试冻结它。如果可行,则逐个添加其他包,并在每一步检查冻结的应用程序。

      编辑二:

      1. 另一种可能是您与PyQt4 存在冲突,如果它已安装在您的系统上。确保从您的应用程序中删除任何对 PyQt4 的导入,并可能在您的 setup.py 脚本中的 build_exe 字典中添加一个条目 excludes: ['PyQt4']

      【讨论】:

      • 但我猜你会收到不同的错误信息。
      • 不知道为什么 cx_Freeze 无法定位 PyQt5。
      • 如果在 python 控制台中输入 import PyQt5.Qt 会发生什么?
      • @M.Denis 我已经在我的回答中添加了进一步的建议。
      • 从包列表中删除 PyQt5 可以消除错误消息。但是在运行应用程序时出现以下属性错误。错误是AttributeError: function 'LLVMPY_OffsetOfElement not found
      猜你喜欢
      • 1970-01-01
      • 2019-07-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-26
      • 2018-08-11
      • 1970-01-01
      相关资源
      最近更新 更多