使用 cx_Freeze 包括 PyQt5 创建 exe 时出现 ValueError

ValueError while creating exe using cx_Freeze including PyQt5

我正在尝试使用 cx_Freeze 创建 .exe。当我尝试构建它时,它会抛出一个值错误。我不明白这到底是什么意思。您认为重新安装 cx_Freeze 可以解决问题吗?

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)]

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'],
    'packages': ['pandas', 'numpy', 'scipy', 'pydub', 'librosa', 'PyQt5.QtCore', 'PyQt5.QtGui', 'PyQt5.QtWidgets',
                 'python_speech_features'],
    }
}

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

构建时,出现以下错误。

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 340, in _GetModuleFinder
finder.IncludeModule(name)
  File "C:\Users\dm\AppData\Local\Programs\Python\Python36\lib\site-packages\cx_Freeze\finder.py", line 651, in IncludeModule
namespace = namespace)
  File "C:\Users\dm\AppData\Local\Programs\Python\Python36\lib\site-packages\cx_Freeze\finder.py", line 311, in _ImportModule
deferredImports, namespace = namespace)
  File "C:\Users\dm\AppData\Local\Programs\Python\Python36\lib\site-packages\cx_Freeze\finder.py", line 385, in _InternalImportModule
namespace = namespace)
  File "C:\Users\dm\AppData\Local\Programs\Python\Python36\lib\site-packages\cx_Freeze\finder.py", line 404, in _InternalImportModule
parentModule, namespace)
  File "C:\Users\dm\AppData\Local\Programs\Python\Python36\lib\site-packages\cx_Freeze\finder.py", line 417, in _LoadModule
namespace)
  File "C:\Users\dm\AppData\Local\Programs\Python\Python36\lib\site-packages\cx_Freeze\finder.py", line 486, in _LoadPackage
self._LoadModule(name, fp, path, info, deferredImports, parent)
  File "C:\Users\dm\AppData\Local\Programs\Python\Python36\lib\site-packages\cx_Freeze\finder.py", line 464, in _LoadModule
self._RunHook("load", module.name, module)
  File "C:\Users\dm\AppData\Local\Programs\Python\Python36\lib\site-packages\cx_Freeze\finder.py", line 537, in _RunHook
method(self, *args)
  File "C:\Users\dm\AppData\Local\Programs\Python\Python36\lib\site-packages\cx_Freeze\hooks.py", line 567, in load_scipy
finder.IncludePackage("scipy.misc")
  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 311, in _ImportModule
deferredImports, namespace = namespace)
  File "C:\Users\dm\AppData\Local\Programs\Python\Python36\lib\site-packages\cx_Freeze\finder.py", line 404, in _InternalImportModule
parentModule, namespace)
  File "C:\Users\dm\AppData\Local\Programs\Python\Python36\lib\site-packages\cx_Freeze\finder.py", line 417, in _LoadModule
namespace)
  File "C:\Users\dm\AppData\Local\Programs\Python\Python36\lib\site-packages\cx_Freeze\finder.py", line 486, in _LoadPackage
self._LoadModule(name, fp, path, info, deferredImports, parent)
  File "C:\Users\dm\AppData\Local\Programs\Python\Python36\lib\site-packages\cx_Freeze\finder.py", line 475, in _LoadModule
self._ScanCode(module.code, module, deferredImports)
  File "C:\Users\dm\AppData\Local\Programs\Python\Python36\lib\site-packages\cx_Freeze\finder.py", line 565, in _ScanCode
module, relativeImportIndex)
  File "C:\Users\dm\AppData\Local\Programs\Python\Python36\lib\site-packages\cx_Freeze\finder.py", line 346, in _ImportModule
deferredImports, namespace = namespace)
  File "C:\Users\dm\AppData\Local\Programs\Python\Python36\lib\site-packages\cx_Freeze\finder.py", line 404, in _InternalImportModule
parentModule, namespace)
  File "C:\Users\dm\AppData\Local\Programs\Python\Python36\lib\site-packages\cx_Freeze\finder.py", line 475, in _LoadModule
self._ScanCode(module.code, module, deferredImports)
  File "C:\Users\dm\AppData\Local\Programs\Python\Python36\lib\site-packages\cx_Freeze\finder.py", line 570, in _ScanCode
fromList, deferredImports)
  File "C:\Users\dm\AppData\Local\Programs\Python\Python36\lib\site-packages\cx_Freeze\finder.py", line 211, in _EnsureFromList
self._ImportModule(subModuleName, deferredImports, caller)
  File "C:\Users\dm\AppData\Local\Programs\Python\Python36\lib\site-packages\cx_Freeze\finder.py", line 311, in _ImportModule
deferredImports, namespace = namespace)
  File "C:\Users\dm\AppData\Local\Programs\Python\Python36\lib\site-packages\cx_Freeze\finder.py", line 404, in _InternalImportModule
parentModule, namespace)
  File "C:\Users\dm\AppData\Local\Programs\Python\Python36\lib\site-packages\cx_Freeze\finder.py", line 475, in _LoadModule
self._ScanCode(module.code, module, deferredImports)
  File "C:\Users\dm\AppData\Local\Programs\Python\Python36\lib\site-packages\cx_Freeze\finder.py", line 589, in _ScanCode
topLevel = False)
  File "C:\Users\dm\AppData\Local\Programs\Python\Python36\lib\site-packages\cx_Freeze\finder.py", line 589, in _ScanCode
topLevel = False)
  File "C:\Users\dm\AppData\Local\Programs\Python\Python36\lib\site-packages\cx_Freeze\finder.py", line 570, in _ScanCode
fromList, deferredImports)
  File "C:\Users\dm\AppData\Local\Programs\Python\Python36\lib\site-packages\cx_Freeze\finder.py", line 211, in _EnsureFromList
self._ImportModule(subModuleName, deferredImports, caller)
  File "C:\Users\dm\AppData\Local\Programs\Python\Python36\lib\site-packages\cx_Freeze\finder.py", line 311, in _ImportModule
deferredImports, namespace = namespace)
  File "C:\Users\dm\AppData\Local\Programs\Python\Python36\lib\site-packages\cx_Freeze\finder.py", line 404, in _InternalImportModule
parentModule, namespace)
  File "C:\Users\dm\AppData\Local\Programs\Python\Python36\lib\site-packages\cx_Freeze\finder.py", line 475, in _LoadModule
self._ScanCode(module.code, module, deferredImports)
  File "C:\Users\dm\AppData\Local\Programs\Python\Python36\lib\site-packages\cx_Freeze\finder.py", line 565, in _ScanCode
module, relativeImportIndex)
  File "C:\Users\dm\AppData\Local\Programs\Python\Python36\lib\site-packages\cx_Freeze\finder.py", line 311, in _ImportModule
deferredImports, namespace = namespace)
  File "C:\Users\dm\AppData\Local\Programs\Python\Python36\lib\site-packages\cx_Freeze\finder.py", line 404, in _InternalImportModule
parentModule, namespace)
  File "C:\Users\dm\AppData\Local\Programs\Python\Python36\lib\site-packages\cx_Freeze\finder.py", line 464, in _LoadModule
self._RunHook("load", module.name, module)
  File "C:\Users\dm\AppData\Local\Programs\Python\Python36\lib\site-packages\cx_Freeze\finder.py", line 537, in _RunHook
method(self, *args)
  File "C:\Users\dm\AppData\Local\Programs\Python\Python36\lib\site-packages\cx_Freeze\hooks.py", line 526, in load_PyQt4_QtGui
name, QtCore = _qt_implementation(module)
  File "C:\Users\dm\AppData\Local\Programs\Python\Python36\lib\site-packages\cx_Freeze\hooks.py", line 437, in _qt_implementation
_qtcore = __import__(name, fromlist=['QtCore']).QtCore
ValueError: PyCapsule_GetPointer called with incorrect name

感谢任何帮助。

您的安装脚本包含属于 tkinterPyQt5 的文件和包,您发布的堆栈跟踪显示安装脚本在执行加载挂钩时失败 PyQt4,这表明您的主脚本在某处导入了 PyQt4。从 tkinterPyQt4PyQt5 中选择一个 GUI 库,并在整个主脚本和设置脚本中仅使用所选的一个。

您可以将不使用的库添加到 build_exe 选项字典中的 'excludes' 列表项,以告知 cx_Freeze 不要包含它们。