Using PyQt5 with opencv-python (cv2) causes error "could not load Qt platform plugin xcb even though it was found"
-
I am running an ubuntu based linux system (pop_os 20.04). I am not connecting remotely to a device. I am using Nvidia xserver driver from my workstation.
Weird thing is, as long as I don't import or use cv2, everything works flawlessly. As soon as I import cv2, I get this error.
This is what the export QT_DEBUG_PLUGINS=1 printed on the command line:
https://pastebin.com/uxm7LZpc -
$ grep libqxcb.so output
QFactoryLoader::QFactoryLoader() looking at "/home/lvlanson/.local/lib/python3.8/site-packages/cv2/qt/plugins/platforms/libqxcb.so"
Found metadata in lib /home/lvlanson/.local/lib/python3.8/site-packages/cv2/qt/plugins/platforms/libqxcb.so, metadata=
QFactoryLoader::QFactoryLoader() looking at "/home/lvlanson/.local/lib/python3.8/site-packages/PyQt5/Qt/plugins/platforms/libqxcb.so"
Found metadata in lib /home/lvlanson/.local/lib/python3.8/site-packages/PyQt5/Qt/plugins/platforms/libqxcb.so, metadata=
loaded library "/home/lvlanson/.local/lib/python3.8/site-packages/cv2/qt/plugins/platforms/libqxcb.so"The QPA plugin loading is finding 2 copies of the XCB QPA plugin, and choosing to load the version packaged with cv2. The version of Qt the plugin comes from appears to be incompatible with the version of Qt attempting to load it. I'm guess that Qt version is from PyQt.
This might be solvable by changing the order of imports, object instantiation, or both. Changing the plugin loading path might be possible, although python modules sometimes hardcode their own idea of where things should come from. Deleting the cv2 version of the plugin might work. If those options are unworkable or unreasonable, you may be stuck rebuilding PyQt and/or OpenCV.
-
@lvlanson I had the same problem. You can install headless opencv without GUI. Then it works.
pip install opencv-python-headless
-
@Hikari-Tretaki thanks for the tip! I had the same problem and switching from
opencv-python
toopencv-python-headless
fixed it for me. -
@Hikari-Tretaki Thank you very much! Your solution work for me too!
-
@Hikari-Tretaki thank you very much!!
-
Hi, The error still persists even when I use "pip install opencv-python-headless"
Error message:
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.
/opt/conda/envs/yashenv2/.tmp8gb_phfb: line 3: 17 Aborted python TAD_GUI_Contoller.py
ERROR conda.cli.main_run:execute(33): Subprocess for 'conda run ['python', 'TAD_GUI_Contoller.py']' command failed. (See above for error)Other info: When I used opencv-python (Error was similar like above except it had - Could not load the Qt platform plugin "xcb" in ".../python3.7/site-packages/cv2/qt/plugins" even though it was found.). I am using docker (with Ubuntu 18 + WSL2). The problem persist only in docker container. (The python codes work fine in windows)
Am I missing some Ubuntu libraries? I tried installing libxcb's and many other libraries. It just does not work. Or is it related to PyQt5?
-
@yashwant said in Using PyQt5 with opencv-python (cv2) causes error "could not load Qt platform plugin xcb even though it was found":
Am I missing some Ubuntu libraries?
Yes.
Set QT_DEBUG_PLUGINS before starting the app and check output. -
@jsulm said in Using PyQt5 with opencv-python (cv2) causes error "could not load Qt platform plugin xcb even though it was found":
Set QT_DEBUG_PLUGINS
Can you tell me how can I set "export QT_DEBUG_PLUGINS =1" in docker container? Should it be written inside Dockerfile? or should I run the container something like docker run -it Image -v export QT_DEBUG_PLUGINS =1 ? or should I be using docker exec ?
-
@yashwant said in Using PyQt5 with opencv-python (cv2) causes error "could not load Qt platform plugin xcb even though it was found":
qt.qpa.xcb: could not connect to display
Are you sure you have an X server running and available?
I am using docker (with Ubuntu 18 + WSL2). The problem persist only in docker container.
Please be aware that you have a (very) unusual environment.
Given that Docker is involved you may need to check how to make the container output work properly under X...
-
@Pablo-J-Rogina said in Using PyQt5 with opencv-python (cv2) causes error "could not load Qt platform plugin xcb even though it was found":
rly under
- It is just standard docker windows setup (It just uses the WSL2). It is the standard installation process.
- How can I check if the Xserver is running?
Thanks !
-
I am just trying to open a python GUI(which opens fine in windows 10) using docker setup. I did install docker for windows using the standard procedure. And I can load the python codes (without .ui - python files without GUI) into the container without any problem. The problem arises only when I try to load python files with GUI.
-
@yashwant said in Using PyQt5 with opencv-python (cv2) causes error "could not load Qt platform plugin xcb even though it was found":
The problem arises only when I try to load python files with GUI.
That's the point. You may need to check how to make a Docker container use the GUI of the host...
-
Replacing opencv-python with opencv-python-headless is one option. However, for some reasons if you want to keep the library, then you could add following lines after import cv2:
import os, sys ci_build_and_not_headless = False try: from cv2.version import ci_build, headless ci_and_not_headless = ci_build and not headless except: pass if sys.platform.startswith("linux") and ci_and_not_headless: os.environ.pop("QT_QPA_PLATFORM_PLUGIN_PATH") if sys.platform.startswith("linux") and ci_and_not_headless: os.environ.pop("QT_QPA_FONTDIR")
This is based on the cv2's init file . What it does is essentially removing environment variables that are added by the init file (This are only done at runtime and does not have permanent effect upon your system.)
-
@Pablo-J-Rogina said in Using PyQt5 with opencv-python (cv2) causes error "could not load Qt platform plugin xcb even though it was found":
UI of the host..
Yes that was my goal (docker with gui)! and above mentioned was my error when I try GUI on docker.! Anyway thanks!
-
@yashwant said in Using PyQt5 with opencv-python (cv2) causes error "could not load Qt platform plugin xcb even though it was found":
that was my goal (docker with gui)!
Yes, I understand that. Please take into account that this issue is not related (or produced by) Qt itself so the solution might come from some other source(s), not only this forum
-
@jeremy_k said in Using PyQt5 with opencv-python (cv2) causes error "could not load Qt platform plugin xcb even though it was found":
worka
Hello guys, so the problem is that
import cv2
setsQT_*
env vars.
I came to this workaround, maybe it will help someone:import cv2 for k, v in os.environ.items(): if k.startswith("QT_") and "cv2" in v: del os.environ[k]