我试图使用cx_freeze打包PyQt应用程序。我在Windows 7上运行Python 3.4,Qt 5.6,PyQt 5.5.1和Cx_freeze 4.3.4。
我的应用程序有三个部分,python / PyQt代码,包含GUI元素的Qt UI文件,以及运行类似于places_map.qml示例的交互式地图的QML文件。使用osm map插件的QML文件。
我的Qt UI文件包含一个QQuickWidget,其源代码是QML文件。我试图打包应用程序,以便其他人可以在不安装python和Qt的情况下运行它。我已经为cx_freeze创建了一个自定义的setup.py脚本。但是,在运行构建的Exe文件时,我一直遇到与QQuickWidget相关的各种ImportErrors。当我的Python代码加载UI文件时发生错误。我试图在我的setup.py文件中包含几乎所有与QQuickWidgets和QML代码相关的内容。最初,ImportError缺少QQuickWidgets。添加SIP QQuickWidget文件后,错误现在与QQuickWidgets.QQuickWidget相关。下面是我的setup.py文件。
from cx_Freeze import setup, Executable
import os
PYQT5_DIR = "c:/Python34/lib/site-packages/PyQt5/"
include_files = ['TTRMS.ui','places_map.qml',
(os.path.join(PYQT5_DIR, "qml", "QtQuick.2"), "QtQuick.2"),
(os.path.join(PYQT5_DIR, "qml", "QtQuick"), "QtQuick"),
(os.path.join(PYQT5_DIR, "qml", "QtQml"), "QtQml"),
(os.path.join(PYQT5_DIR, "qml", "Qt"), "Qt"),
(os.path.join(PYQT5_DIR, "qml", "QtPositioning"), "QtPositioning"),
(os.path.join(PYQT5_DIR, "qml", "QtLocation"), "QtLocation"),'C:/Python34/Lib/site-packages/PyQt5/uic/widget-plugins',
'C:/Python34/Lib/site-packages/PyQt5/plugins/geoservices','C:/Python34/Lib/site-packages/PyQt5/sip/PyQt5/QtQuickWidgets',
'C:/Python34/Lib/site-packages/PyQt5/sip/PyQt5/QtQuick']
buildOptions = dict(packages = ['PyQt5.QtQuickWidgets',"atexit","sip","PyQt5.QtCore","PyQt5.QtGui","PyQt5.QtWidgets",
"PyQt5.QtNetwork","PyQt5.QtOpenGL", "PyQt5.QtQml", "PyQt5.QtQuick"],
excludes = [], includes = ["atexit","re"], include_files = include_files)
import sys
base = 'Win32GUI' if sys.platform=='win32' else None
executables = [
Executable('Main.py', base=base, targetName = 'main.exe')
]
setup(name='TTRMS',
version = '1.0',
description = 'Travel Time Reliability',
options = dict(build_exe = buildOptions),
executables = executables)
运行我的Exe时出现错误:
我不确定还需要在setup.py中包含哪些内容才能正确打包应用程序。我已经成功打包了一个带有UI文件的小型PyQt应用程序,但从来没有一个UI文件,然后通过QQuickWidget导入QML。我对Qt也不是很了解,所以整个过程对我来说都是新的。如果您有任何建议,请告诉我,如果我需要澄清任何问题,请告诉我。我感谢任何帮助!