python + QML程序中调用WebView后打包

QML中如果加入了WebView控件,在用pyinstaller打包时不会自动导入,从而导致打包出的程序运行报错,no WebView plugin found,此时需要手动将WebView控件复制到打包后的程序目录下的PySide6包中,需要复制的文件有以下四个:

  • xxx/Lib/site-packages/PySide6/QtWebEngineProcess.exe
  • xxx/Lib/site-packages/PySide6/plugins/webview
  • xxx/Lib/site-packages/PySide6/resources
  • xxx/Lib/site-packages/PySide6/translations/qtwebengine_locales

其中xxx为python程序安装目录,例如第一个文件,我的完整路径为 'C:/myenvs/Python/Lib/site-packages/PySide6/QtWebEngineProcess.exe' ,打包后的程序目录中会有一个PySide6文件夹,将这些文件或文件夹复制到对应的位置就可以了,例如第二行的webview文件夹放到 '打包后的程序目录/PySide6/plugin' 文件夹下,其他同理。

另外,还可以通过修改打包时的.spec文件来自动拷贝这些文件,用TXT或其他文本编辑器打开.spec文件,其中 'a = Analysis(' 后面会有一个 'datas=[]' ,在datas这个数组中加入元组,元组的第一个元素代表from来自哪里,第二个元素代表to要拷贝到哪里,需要注意的是如果要拷贝的是单个文件,to后面的目标地址写到该文件所在的文件夹即可,具体例子见下面的main.spec文件.

main.spec

# -*- mode: python ; coding: utf-8 -*-


block_cipher = None


a = Analysis(
    ['main.py'],
    pathex=[],
    binaries=[],
    datas=[('./main.qml', '.'), 
            ('./assets', 'assets'),
            ('./temp', 'temp'),
            ('C:/myenvs/Python/Lib/site-packages/PySide6/plugins/webview', 'PySide6/plugins/webview'), 
            ('C:/myenvs/Python/Lib/site-packages/PySide6/QtWebEngineProcess.exe', 'PySide6'), 
            ('C:/myenvs/Python/Lib/site-packages/PySide6/resources', 'PySide6/resources'),
            ('C:/myenvs/Python/Lib/site-packages/PySide6/translations/qtwebengine_locales', 'PySide6/translations/qtwebengine_locales')],
    hiddenimports=[],
    hookspath=[],
    hooksconfig={},
    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='main',
    debug=False,
    bootloader_ignore_signals=False,
    strip=False,
    upx=True,
    console=True,
    disable_windowed_traceback=False,
    argv_emulation=False,
    target_arch=None,
    codesign_identity=None,
    entitlements_file=None,
)
coll = COLLECT(
    exe,
    a.binaries,
    a.zipfiles,
    a.datas,
    strip=False,
    upx=True,
    upx_exclude=[],
    name='main',
)
posted @ 2023-02-20 19:46  暮鼓晨钟·  阅读(287)  评论(0编辑  收藏  举报