Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Using PyQt5 with opencv-python (cv2) causes error "could not load Qt platform plugin xcb even though it was found"
Forum Update on Monday, May 27th 2025

Using PyQt5 with opencv-python (cv2) causes error "could not load Qt platform plugin xcb even though it was found"

Scheduled Pinned Locked Moved Solved Qt for Python
30 Posts 19 Posters 69.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Y yashwant

    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?

    Pablo J. RoginaP Offline
    Pablo J. RoginaP Offline
    Pablo J. Rogina
    wrote on last edited by Pablo J. Rogina
    #16

    @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...

    Upvote the answer(s) that helped you solve the issue
    Use "Topic Tools" button to mark your post as Solved
    Add screenshots via postimage.org
    Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

    Y 1 Reply Last reply
    0
    • Pablo J. RoginaP Pablo J. Rogina

      @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...

      Y Offline
      Y Offline
      yashwant
      wrote on last edited by
      #17

      @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

      1. It is just standard docker windows setup (It just uses the WSL2). It is the standard installation process.
      2. How can I check if the Xserver is running?

      Thanks !

      1 Reply Last reply
      0
      • Y Offline
        Y Offline
        yashwant
        wrote on last edited by
        #18

        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.

        Pablo J. RoginaP 1 Reply Last reply
        0
        • Y yashwant

          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.

          Pablo J. RoginaP Offline
          Pablo J. RoginaP Offline
          Pablo J. Rogina
          wrote on last edited by
          #19

          @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...

          Upvote the answer(s) that helped you solve the issue
          Use "Topic Tools" button to mark your post as Solved
          Add screenshots via postimage.org
          Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

          Y 1 Reply Last reply
          0
          • A Offline
            A Offline
            alphaomega
            wrote on last edited by alphaomega
            #20

            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.)

            1 Reply Last reply
            1
            • Pablo J. RoginaP Pablo J. Rogina

              @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...

              Y Offline
              Y Offline
              yashwant
              wrote on last edited by
              #21

              @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!

              Pablo J. RoginaP 1 Reply Last reply
              0
              • Y yashwant

                @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!

                Pablo J. RoginaP Offline
                Pablo J. RoginaP Offline
                Pablo J. Rogina
                wrote on last edited by
                #22

                @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

                Upvote the answer(s) that helped you solve the issue
                Use "Topic Tools" button to mark your post as Solved
                Add screenshots via postimage.org
                Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                1 Reply Last reply
                0
                • R Offline
                  R Offline
                  redeyed
                  wrote on last edited by redeyed
                  #23

                  @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 sets QT_* 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]
                  
                  A 1 Reply Last reply
                  4
                  • O Offline
                    O Offline
                    OBaladi
                    wrote on last edited by
                    #24

                    I solved the problem by downgrading the cv2 version, simply run
                    python3 -m pip install opencv-contrib-python==4.1.0.25
                    i used to have the 4.5.3.56

                    1 Reply Last reply
                    0
                    • L lvlanson

                      Hey there,

                      I am working on a big project, creating a GUI for working with convolutional neural networks. I need to use cv2 for image processing. For some reason I cannot import cv2 without my application creating an error.

                      I am using pycharm as an IDE which is handling my virtual environment via pip. The librarys I use are the following:

                      • PyQt5
                      • pyqtchart
                      • pyyaml
                      • Pillow
                      • tensorflow
                      • distro
                      • numpy
                      • sklearn
                      • pandas
                      • opencv-python

                      When I use cv2 I get the following error-message:

                      qt.qpa.plugin: Could not load the Qt platform plugin "xcb" in "$HOME/Documents/gui_nn/venv/lib/python3.8/site-packages/cv2/qt/plugins" 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: xcb, eglfs, linuxfb, minimal, minimalegl, offscreen, vnc, wayland-egl, wayland, wayland-xcomposite-egl, wayland-xcomposite-glx, webgl.

                      eyllanescE Offline
                      eyllanescE Offline
                      eyllanesc
                      wrote on last edited by
                      #25

                      @lvlanson Also see https://stackoverflow.com/a/68417901/6622587

                      If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

                      1 Reply Last reply
                      0
                      • T Offline
                        T Offline
                        Thiago Oliveira
                        wrote on last edited by
                        #26

                        @Hikari-Tretaki, muito obrigado por sua ajuda. Também consegui resolver o meu problema com sua dica.

                        1 Reply Last reply
                        0
                        • R redeyed

                          @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 sets QT_* 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]
                          
                          A Offline
                          A Offline
                          aria212019005
                          wrote on last edited by
                          #27

                          @redeyed Thankyou, I was confused around installing

                          opencv-python
                          

                          and

                          opencv-python-headless
                          

                          before this. But it ends with "we need both libraries, and this"

                          1 Reply Last reply
                          0
                          • H Hikari Tretaki

                            @lvlanson I had the same problem. You can install headless opencv without GUI. Then it works.

                            pip install opencv-python-headless

                            V Offline
                            V Offline
                            Vijay Kumar
                            wrote on last edited by
                            #28

                            @Hikari-Tretaki Thank you so much, installing opencv-python-headless fixed the issue

                            1 Reply Last reply
                            0
                            • H Hikari Tretaki

                              @lvlanson I had the same problem. You can install headless opencv without GUI. Then it works.

                              pip install opencv-python-headless

                              V Offline
                              V Offline
                              Vitor Dalla Vecchia
                              wrote on last edited by
                              #29

                              @Hikari-Tretaki MAN, I LOVE YOU SO MUCH. THANKS!

                              1 Reply Last reply
                              0
                              • H Offline
                                H Offline
                                hitbuyi
                                wrote on last edited by hitbuyi
                                #30

                                I also have this problem, my environments are
                                1, OS: ubuntu20.04
                                2, conda: 4.13
                                3, PyQt5: 5.15.2
                                4, local Qt5: 5.15.6
                                5, opencv- python 4.5.x

                                It really takes several days to find a solution, @jeremy_k gave almost the solution. I use PyQt5 + OpenCV-python on conda virtual environment,
                                summarize the solutions :

                                1, delete cv2/libqxcb.so, it works for pyqt5 which has its own version of Qt, but opencn-python doesn't work for other application which isn't relevant to PyQt5

                                2, if pyside2 is installed, pyside2 has its owen version of Qt, so add below code at the beginning of python program

                                ```
                                import PySide2
                                dirname = os.path.dirname(PySide2.__file__)
                                plugin_path = os.path.join(dirname, 'plugins', 'platforms')
                                os.environ['QT_QPA_PLATFORM_PLUGIN_PATH'] = plugin_path
                                ```
                                

                                this solution works, at expense of intalling pyside2 which is 400M+

                                3, PyQt5 has its own version of Qt,similiar to above, add below code at the beginning of python program

                                 ```
                                import PyQt5
                                 dirname = os.path.dirname(PyQt5.__file__)
                                 plugin_path = os.path.join(dirname, 'Qt5','plugins', 'platforms')
                                 os.environ['QT_QPA_PLATFORM_PLUGIN_PATH'] = plugin_path
                                
                                this soltuion is fine,
                                1 Reply Last reply
                                0

                                • Login

                                • Login or register to search.
                                • First post
                                  Last post
                                0
                                • Categories
                                • Recent
                                • Tags
                                • Popular
                                • Users
                                • Groups
                                • Search
                                • Get Qt Extensions
                                • Unsolved