cx_Freeze: ImportError: No module named 'PyQt5.Qt'

女生的网名这么多〃 提交于 2020-01-30 05:42:08

问题


I am trying to build my GUI application using cx_Freeze. The setup.py is as follows:

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)

While building, the cx_Freeze is through the following error. I have installed 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'

Could anyone help me to figre out what is actually going wrong?


回答1:


I know this is an old question, but today I faced this problem.

This is how I solved it:

Removed PyQt5 and everything that was related to it from my setup.py ('{"build_exe": {"packages":' <- this part). After it compiled my exe. If you run the exe and theres a missing PyQt5 module error, then from your site-packages directory (path similar to this: d:\Python37\Lib\site-packages\PyQt5\) copy the whole PyQt5 directory to your cx_freeze built lib directory (example: ..\build\exe.win-amd64-3.7\lib\)

Now try to run the exe and there should be no missing module error, at least related to PyQt5. If you have any other missing module problem, then just copy it from your site-packages into the lib directory. Hope it helps.




回答2:


Try to remove the (unnecessary?) line

import PyQt5.Qt

from your setup.py script.

EDIT after the OP removed this line:

  1. Try to remove the unnecessary os.environ statements, these are for tkinter, maybe they conflict. Remove the 3 DLLs entries in the include_files (keep only the icon). Add 'atexit' to the includes list, see the cx_Freeze PyQt5 example.

  2. Try to re-install PyQt5 and cx_Freeze, see potential caveats in ImportError: No module named PytQt5 and PyQt5 and QtGui module not found.

  3. If this still does not work, maybe there is a conflict with another package used in your application. To find out, make a minimal example using only PyQt5, such as the cx_Freeze PyQt5 example and try to freeze it. If it works, add the other packages one by one, checking the frozen application at each step.

EDIT II:

  1. A further possibility is that you have a conflict with PyQt4 if it is or has been installed on your system. Make sure to remove any import of PyQt4 from your application and maybe add an entry excludes: ['PyQt4'] to the build_exe dictionary in your setup.py script.


来源:https://stackoverflow.com/questions/55120281/cx-freeze-importerror-no-module-named-pyqt5-qt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!