使用Nuitka编译含QtWebEngineView的PySide2程序后运行异常
Nuitka编译PySide2 QWebEngineView程序崩溃问题
问题描述
使用PySide2的QWebEngineView实现网页窗口展示,Python IDLE中运行完全正常,但使用Nuitka编译后程序崩溃;移除Web模块仅保留普通窗口时,编译运行正常。
相关代码
from PySide2 import QtCore, QtGui, QtWidgets from PySide2.QtWidgets import QApplication, QMainWindow, QPushButton, QVBoxLayout, QWidget from PySide2.QtWebEngineWidgets import QWebEngineView import logging class Ui_MainWindow(object): def setupUi(self, MainWindow): MainWindow.setObjectName("MainWindow") MainWindow.resize(1000, 700) MainWindow.setBaseSize(QtCore.QSize(0, 0)) # window self.centralwidget = QtWidgets.QWidget(MainWindow) MainWindow.setCentralWidget(self.centralwidget) self.centralwidget.setObjectName("centralwidget") font = QtGui.QFont() font.setFamily("Segoe UI Semibold") self.main_window_frame = QtWidgets.QFrame(self.centralwidget) self.main_window_frame.setGeometry(QtCore.QRect(5, 10, 990, 680)) self.main_window_frame.setStyleSheet("background-color: rgb(54,57,62)") self.main_window_frame.setFrameShape(QtWidgets.QFrame.StyledPanel) self.main_window_frame.setFrameShadow(QtWidgets.QFrame.Raised) self.main_window_frame.setObjectName("main_window_frame") # tab menu frame self.tab_menu_frame3 = QtWidgets.QFrame(self.main_window_frame) self.tab_menu_frame3.setGeometry(QtCore.QRect(0, 50, 990, 630)) self.tab_menu_frame3.setStyleSheet("background-color: rgb(54,57,62)") self.tab_menu_frame3.setFrameShape(QtWidgets.QFrame.StyledPanel) self.tab_menu_frame3.setFrameShadow(QtWidgets.QFrame.Raised) self.tab_menu_frame3.setObjectName("tab_menu_frame3") # web engine self.web_view = QWebEngineView(self.main_window_frame) self.web_view.setGeometry(QtCore.QRect(0, 50, 990, 630)) self.web_view.setUrl(QtCore.QUrl('https://example.com')) try: self.web_view.show() except Exception as e: logging.error(f"Error: {e}") class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow): def __init__(self): super().__init__() self.setupUi(self) self.setWindowFlags(QtCore.Qt.FramelessWindowHint) self.setAttribute(QtCore.Qt.WA_TranslucentBackground) if __name__ == '__main__': import sys app = QtWidgets.QApplication(sys.argv) try: w = MainWindow() w.show() sys.exit(app.exec_()) except Exception as e: logging.error(f"Error: {e}")
Nuitka编译参数
python -m nuitka --onefile --windows-console-mode=force --enable-plugin=pyside2 --standalone test.pyw
运行报错信息
QtWebEngine resources not found at C:/Temp/ONEFIL~1/PySide2/resources... Trying parent directory… Qt WebEngine resources not found at C:/Temp/ONEFIL~1/PySide2... Trying application directory… Qt WebEngine resources not found at C:/Temp/onefile_9968_133754483410059208. Trying fallback directory… The application MAY NOT work. Installed Qt WebEngine locales directory not found at location C:/Temp/ONEFIL~1/PySide2/translations\qtwebengine_locales Trying application directory… Qt WebEngine locales directory not found at location C:/Temp/onefile_9968_133754483410059208\qtwebengine_locales. Trying fallback directory… Translations MAY NOT not be correct. [1107/151902.529:ERROR: icu_util.cc (251)] Couldn't mmap icu data file
解决方法
方法1:改用目录模式编译(最稳妥)
单文件模式下QtWebEngine的资源文件无法被正确定位,建议禁用单文件模式,改用目录模式编译:
python -m nuitka --windows-console-mode=force --enable-plugin=pyside2 --standalone test.pyw
编译完成后会生成test.dist目录,直接运行目录内的test.exe即可正常启动程序。
方法2:单文件模式下手动配置资源路径
若必须使用单文件模式,需修改代码并调整编译参数:
- 修改代码,在
if __name__ == '__main__':开头添加资源路径配置:
import os import sys from PySide2.QtCore import QCoreApplication # 处理Nuitka单文件模式的资源路径问题 if hasattr(sys, '_MEIPASS'): # 指定QtWebEngine资源目录 qt_resources_path = os.path.join(sys._MEIPASS, 'PySide2', 'Qt', 'resources') QCoreApplication.setLibraryPaths([qt_resources_path]) # 设置ICU数据文件路径 icu_file = os.path.join(qt_resources_path, 'icudtl.dat') os.environ['QTWEBENGINE_CHROMIUM_FLAGS'] = f'--icu-data-file={icu_file}'
- 编译时添加
--include-data-dir参数,确保资源文件被打包:
找到你的PySide2安装目录(如虚拟环境下的venv\Lib\site-packages\PySide2),替换路径后执行:
python -m nuitka --onefile --windows-console-mode=force --enable-plugin=pyside2 --standalone --include-data-dir="C:\your_pyside2_path\PySide2\Qt\resources=PySide2/Qt/resources" test.pyw
方法3:确保PySide2完整安装
若PySide2的QtWebEngine资源文件缺失,重新安装完整包:
pip uninstall -y PySide2 pip install PySide2 --no-cache-dir
内容的提问来源于stack exchange,提问作者Aofrue
相关产品推荐
相关产品推荐

