在Ubuntu(WSL2)上使用PyQt5时,GUI周围出现错误


当我尝试在wsl2中使用PyQt5时,遇到了麻烦,因此备忘了。
该日文文章说,如果只安装一个apt,就可以解决,但我没有。

前提

  • 我想使用PyQt5
  • 这里的环境是wsl2上的Ubuntu 18.04,但在Ubuntu上应该很常见。
  • 假定已经建立了wsl(2)的GUI环境。就像放入Xserver。

    • 如果您想在这里了解,还请参见WSL2的介绍,GUI环境的构建以及sshfs的gnome-open!

麻烦

1
2
3
4
5
6
7
$ main.py
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

在各种日语页面上写着什么

1
sudo apt install libxkbcommon-x11-0

这还不够。这也是必要的!

首先,找出libqeglfs.so在哪里。

1
find / -name libqeglfs.so

等但是,执行时间可能很困难。
例如

1
export QT_DEBUG_PLUGINS=1

将其作为

置于调试模式,并执行目标Python文件

1
2
3
4
Got keys from plugin meta data ("xcb")
QFactoryLoader::QFactoryLoader() checking directory path "/path/to/python/bin/platforms" ...
Cannot load library /path/to/python/site-packages/PyQt5/Qt/plugins/platforms/libqxcb.so: (libxcb-icccm.so.4: cannot open shared object file: No such file or directory)
QLibraryPrivate::loadPlugin failed on "/path/to/python/site-packages/PyQt5/Qt/plugins/platforms/libqxcb.so" : "Cannot load library /home/path/to/python/site-packages/PyQt5/Qt/plugins/platforms/libqxcb.so: (libxcb-icccm.so.4: cannot open shared object file: No such file or directory)"

诸如

之类的内容会在结尾出现(Python的路径已被重写为path/to/python)。

如果找到此内容,
https://forum.qt.io/topic/115732/could-not-load-the-qt-platform-plugin-xcb-in-even-though-it-was-found已找到
依此类推,

1
ldd /path/to/python/site-packages/PyQt5/Qt/plugins/platforms/libqxcb.so

您应该执行

。如果很难看到,可以添加|grep "not found"

这将告诉您缺少的内容,例如,我是

1
2
3
4
5
6
7
8
9
10
11
$ ldd /path/to/python/site-packages/PyQt5/Qt/plugins/platforms/libqxcb.so | grep "not found"
        libxcb-icccm.so.4 => not found
        libxcb-image.so.0 => not found
        libxcb-keysyms.so.1 => not found
        libxcb-render-util.so.0 => not found
        libxcb-xinerama.so.0 => not found
        libxcb-icccm.so.4 => not found
        libxcb-image.so.0 => not found
        libxcb-keysyms.so.1 => not found
        libxcb-render-util.so.0 => not found
        libxcb-xinerama.so.0 => not found

,所以

1
2
3
4
5
6
sudo apt install libxkbcommon-x11-0
sudo apt install libxcb-icccm4
sudo apt install libxcb-image0
sudo apt install libxcb-keysyms1
sudo apt install libxcb-render-util0
sudo apt install libxcb-xinerama0

完成

之后,它可以工作。恭喜你