在 Ubuntu 中运行 PyQt5
在 Ubuntu 上运行 PyQt5 应用程序时,我遇到了这个 Qt 平台插件错误:
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.
在我的情况中,应用程序可以找到 xcb Qt 平台插件,但该插件所需的某个库缺失了。安装缺失的 xcb 依赖后,问题就解决了。

一个有用的初步检查方法,是在启用 Qt 插件调试的情况下运行应用程序:
QT_DEBUG_PLUGINS=1 python3 your_app.py
输出通常会显示哪个共享库加载失败。在许多 Ubuntu 安装环境中,缺失的包是某个 libxcb 运行时库。一个常见的修复方法是:
sudo apt update
sudo apt install libxcb-xinerama0
如果错误仍然存在,请安装 QT_DEBUG_PLUGINS=1 报告的具体缺失库所对应的软件包。可以用下面的命令搜索:
apt-file search name-of-missing-library.so
如果尚未安装 apt-file:
sudo apt install apt-file
sudo apt-file update
安装缺失的库之后,再次运行 PyQt5 应用程序。如果 xcb 插件能够加载它的所有依赖,应用程序就应该能正常启动。
