PyInstaller 打包的 PySide6 应用程序无法加载 qtquick2plugin.dll

问题描述

我有一个 PySide6 + QML 应用程序,可以使用 PyInstaller 打包成 exe 文件

这是我的代码

main.qml

sudo postfix set-permissions

main.py

import QtQuick
import QtQuick.Layouts
import QtQuick.Controls

applicationwindow {
    id: window
    title: "Motor"
    width: 500
    height: 600
    visible: true

    maximumHeight: height
    maximumWidth: width

    minimumHeight: height
    minimumWidth: width
}

电机规格

from PySide6.QtGui import QGuiApplication
from PySide6.QtQml import QQmlApplicationEngine

if __name__ == "__main__":
    import sys 
    app = QGuiApplication(sys.argv)
    engine = QQmlApplicationEngine(parent=app)
  
    engine.load("main.qml")
    sys.exit(app.exec_())
    if not engine.rootObjects():
        sys.exit(-1)
    sys.exit(app.exec_())

# -*- mode: python ; coding: utf-8 -*- block_cipher = None a = Analysis(['main.py'],pathex=['E:\\Projects\\motor\\GUI'],binaries=[],datas=[('main.qml','.'),('settings.ini','.')],hiddenimports=['PySide6.QtCore','PySide6.QtGui','PySide6.QtQml'],hookspath=[],runtime_hooks=[],excludes=[],win_no_prefer_redirects=False,win_private_assemblies=False,cipher=block_cipher,noarchive=False) pyz = PYZ(a.pure,a.zipped_data,cipher=block_cipher) exe = EXE(pyz,a.scripts,[],exclude_binaries=True,name='MotorGUI',debug=False,bootloader_ignore_signals=False,strip=False,upx=True,console=True ) coll = COLLECT(exe,a.binaries,a.zipfiles,a.datas,upx_exclude=[],name='MotorGUI') 打包后,我将 pyinstaller motor.specpluginsqml 文件夹从 python/site-pack/PySide6 文件夹复制到 exe 的 PySide6 文件夹。

当我运行 exe 文件时,我得到:

translations

QQmlApplicationEngin Failed to load component file:///E:/Projects/motor/GUI/dist/MotorGUI/main.qml:1:1: Cannot load library E:\Projects\motor\GUI\dist\MotorGUI\PySide6\qml\QtQuick\qtquick2plugin.dll 确实存在。

我的python版本是3.8.6

PySide==6.0.0

pyinstaller==4.2

解决方法

PyInstaller(从 4.2 开始)尚不支持 PySide6。

https://github.com/pyinstaller/pyinstaller/issues/5414

,

我找到了根本原因。缺少QT的dll。

我通过将 pyside Qt 文件夹中的所有 .dll 文件复制到 exe 文件夹来解决此问题。

然后它完美运行。

之后,我将多余的dll一一删除,以确定最低要求。

最后在spec文件中添加dll