cx_Freeze:ImportError:没有名为'PyQt5.Qt'的模块

问题描述 投票:1回答:1

我正在尝试使用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'

谁能帮我弄清楚究竟出了什么问题?

python-3.x pyqt5 cx-freeze
1个回答
0
投票

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

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导入,并可能在excludes: ['PyQt4']脚本中的build_exe字典中添加条目setup.py

0
投票

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

这就是我解决它的方式:

从我的setup.py ('{"build_exe": {"packages":' <- this part).中删除了PyQt5以及与之相关的所有内容。如果您运行exe并且缺少PyQt5模块错误,那么从您的site-packages目录(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目录中。希望能帮助到你。

© www.soinside.com 2019 - 2024. All rights reserved.