You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Ubuntu 24.04下PyQt5应用启动失败的问题求助

Ubuntu 24.04下PyQt5应用启动失败的问题求助

我在Ubuntu 24.04上部署PyQt5应用时遇到了启动失败的问题,运行程序后弹出了以下错误信息:

Warning: Ignoring XDG_SESSION_TYPE=wayland on Gnome. Use QT_QPA_PLATFORM=wayland to run on Wayland anyway.
qt.qpa.plugin: Could not load the Qt platform plugin "wayland" in "/usr/lib/x86_64-linux-gnu/qt5/plugins/platforms" even though it was found.
This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.

Available platform plugins are: eglfs (from /usr/lib/x86_64-linux-gnu/qt5/plugins/platforms), linuxfb (from /usr/lib/x86_64-linux-gnu/qt5/plugins/platforms), minimal (from /usr/lib/x86_64-linux-gnu/qt5/plugins/platforms), minimalegl (from /usr/lib/x86_64-linux-gnu/qt5/plugins/platforms), offscreen (from /usr/lib/x86_64-linux-gnu/qt5/plugins/platforms), vnc (from /usr/lib/x86_64-linux-gnu/qt5/plugins/platforms), wayland-egl (from /usr/lib/x86_64-linux-gnu/qt5/plugins/platforms)...

针对这个问题,我整理了几个可行的解决办法,你可以逐一尝试:

  • 强制使用X11平台:Ubuntu 24.04默认采用Wayland桌面环境,但PyQt5对Wayland的兼容可能存在问题。你可以临时设置环境变量强制程序使用X11启动:
    在终端中先执行:

    export QT_QPA_PLATFORM=xcb
    

    再启动你的Python应用。如果想让设置永久生效,可将这条命令添加到~/.bashrc(或~/.zshrc,取决于你使用的shell)文件末尾,保存后执行source ~/.bashrc使配置生效。

  • 安装Wayland相关依赖:如果希望在Wayland环境下运行,可尝试安装缺失的Wayland平台插件依赖:

    sudo apt install qtwayland5 qtwayland5-examples
    

    安装完成后,按照提示设置环境变量QT_QPA_PLATFORM=wayland再启动程序。

  • 重新安装PyQt5及相关组件:组件损坏也可能导致这类初始化失败问题,先清理现有安装,再重新安装:

    # 卸载pip安装的PyQt5
    pip3 uninstall pyqt5 pyqt5-sip -y
    # 卸载系统包管理安装的PyQt5
    sudo apt purge python3-pyqt5 -y
    sudo apt autoremove -y
    # 重新安装系统版本的PyQt5
    sudo apt install python3-pyqt5 -y
    

    或者根据你的需求,用pip重新安装:

    pip3 install pyqt5
    
  • 检查Qt插件路径配置:确保Qt的插件路径被系统正确识别,先查看当前路径设置:

    echo $QT_PLUGIN_PATH
    

    如果输出为空,手动设置插件路径:

    export QT_PLUGIN_PATH=/usr/lib/x86_64-linux-gnu/qt5/plugins
    

    同样可以将这条命令添加到shell配置文件中实现永久生效。

备注:内容来源于stack exchange,提问作者Bryan Dickman

火山引擎 最新活动