PyQt报错:Cannot load backend 'Qt5Agg' which requires the 'qt5' interactive framework, as 'headless' is currently running

PyQt报错:could not load Qt platform plugin "xcb" even though it was found

问题描述

在远程链接ubuntu虚拟机进行开发时,报错。

qt.qpa.xcb: could not connect to display 
qt.qpa.plugin: Could not load the Qt platform plugin "xcb" in "" 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, minimal, minimalegl, offscreen, vnc, webgl, xcb.

解决方案

原因1:

pyqt需要绘制UI,而使用远程链接的终端(如windows powershell、xshell、vscode terminal等)不具有图形协议(大概),所以必须回到ubuntu虚拟机上执行代码。

原因2:

参考:Ubuntu18.04下解决Qt出现qt.qpa.plugin:Could not load the Qt platform plugin “xcb“问题_qt.qpa.plugin xcb-CSDN博客

修改配置文件:

sudo vim ~/.bashrc

在最末尾添加如下语句,会在qt启动时列出详细的调试信息:

export QT_DEBUG_PLUGINS=1

激活配置文件:

source ~/.bashrc

重新运行py代码,得到详细的错误信息:

image

按照提示的路径,可以去这个libqxcb.so所在目录查看一下这个libqxcb.so到底要链接哪些动态库:

cd /home/hzy/miniconda3/envs/ml/lib/python3.9/site-packages/PyQt5/Qt5/plugins/platforms/
ls | grep libqxcb.so
ldd libqxcb.so

发现不存在libxcb-xinerama.so.0。于是我们手动安装libxcb-xinerama.so.0库

# 如果还存在其他依赖库没有安装,也一并安装。
sudo apt install libxcb-xinerama0

原因3:

先检查环境变量:PyQt报错:qt.qpa.plugin: Could not find the Qt platform plugin - 3的4次方 - 博客园 (cnblogs.com)

如果添加环境变量后还不行,则查看是否安装了opencv-python,实际上应该安装opencv(是否需要其他依赖我不清楚,如果是使用conda安装的就不用管这么多了)

conda list | grep opencv-python
# 如果安装了,则卸载opencv-python,改为安装opencv
conda uninstall opencv-python
conda install opencv

如果还不能解决,尝试运行代码时使用sudo,至于为什么需要管理员权限,暂时未知。linux解决加载Qt xcb plugins失败 - 倚剑天下 - 博客园 (cnblogs.com)

posted @ 2023-12-26 14:58  3的4次方  阅读(261)  评论(0编辑  收藏  举报