Ubuntu16.04下Python程序出现错误qt.qpa.plugin: Could not load the Qt platform plugin xcb解决方法

问题描述

我在运行一个使用Pyqt5库的Python程序时,出现报错:

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, linuxfb, minimal, minimalegl, offscreen, vnc, wayland-egl, wayland, wayland-xcomposite-egl, wayland-xcomposite-glx, webgl, xcb.

Aborted (core dumped)

解决方案

出现这个问题的可能原因主要是Qt动态链接库的问题,从问题描述来看,应该是libxcb.so库链接不正确,并且说的是“能够找到,但是无法加载”。

尝试方法

你可以先尝试安装一些依赖,看下能不能解决:

sudo apt-get install libxkbcommon-x11-0

最终成功方案

关于这个问题的解决方案,网上有很多都是讲解如何设置链接库,或者如何将platform/plugins等地址加入到环境变量中,我试了一下都没用。

最终经过另一番搜索,突然发现这个链接中有人提到可以在你运行你的Python可执行文件之前先在终端执行下面命令:

export QT_DEBUG_PLUGINS=1

然后再执行你的可执行文件,这个时候就会列出详细的报错原因。

果不其然,此时我再运行程序,列出了一个详细的错误原因的线索:

error while loading shared libraries: libxcb-xinerama.so: cannot open shared object file: No such file or directory

也就是当加载libxcb.so库时,还需要加载libxcb-xinerama库,于是我就安装了这个库:

sudo apt-get install libxcb-xinerama0

然后再执行可执行文件,OK,成功~

其他还可能能安装的库包括:

sudo apt install libxcb-icccm4
sudo apt install libxcb-image0
sudo apt install libxcb-keysyms1
sudo apt install libxcb-render-util0

其他可能问题

qt.qpa.xcb: could not connect to display :0

一般是你连接远程服务器安装一些需要用到图像化界面的软件时会碰到,例如我在安装mayavi就碰到这个问题。
解决方案
首先确认你的ssh连接开启了X11 Forwarding功能,即你在终端执行xclock是否会在显示器上显示一个小时钟;
如果不能可能需要设置一下DISPLAY变量:

export DISPLAY=:0.0

可能需要加上你的电脑IP等信息,具体请查询下;
参考:https://blog.csdn.net/qq_34907927/article/details/116597908

还有可能需要安装:

sudo apt-get install freeglut3-dev

或者降低你的PyQt版本和python-opencv版本:

pip install opencv-python==4.3.0.36 PyQt5==5.15.2

具体到底是上面哪些操作起了作用我也不是很确定,反正最后我是一通操作后成功了。

【参考】

  1. https://blog.csdn.net/jiguangfan/article/details/86490160
  2. https://forum.qt.io/topic/111553/solved-qt-qpa-plugin-could-not-find-the-qt-platform-plugin-xcb-in
  3. http://www.manongjc.com/article/44387.html
  4. https://stackoverflow.com/questions/17106315/failed-to-load-platform-plugin-xcb-while-launching-qt5-app-on-linux-without#
  5. https://github.com/therecipe/qt/issues/775
评论 30
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值