40

I am stuck trying to run a very simple Python script, getting this error:

qt.qpa.plugin: Could not find the Qt platform plugin "cocoa" in ""
This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.

zsh: abort      python3 mypuppy1.py

The script code is:

import cv2
img = cv2.imread('00-puppy.jpg')
while True:
    cv2.imshow('Puppy',img)
    if cv2.waitKey(1) & 0xFF == 27:
        break
cv2.destroyAllWindows()

However this Notebook code works in JupyterLab:

import cv2
img = cv2.imread('00-puppy.jpg')
cv2.imshow('Puppy', img)
cv2.waitKey()

I am on macOS, using Anaconda and JupyterLab. I would appreciate any help with this issue. Thanks!

Hagbard
  • 3,430
  • 5
  • 28
  • 64
Nick Foley
  • 437
  • 1
  • 5
  • 8
  • 1
    I am having the same problem. If you run `brew info qt` you can see where it is installed and the version. I think the problem is python isn't looking for it in the right location so an environment variable must be set. – superhero Feb 04 '20 at 17:08
  • I had to reinstall QT via https://www.qt.io/download-qt-installer to a directory in my `Users/` path and it now works. – superhero Feb 04 '20 at 20:44
  • 2
    Installing `opencv-python-headless` instead of `opencv-python` solved the same issue I had on my Mac (I just did `pip install opencv-python-headless`). I think it is worthwhile to try. – hideya Feb 09 '20 at 14:26
  • I solved my same problem by this https://stackoverflow.com/a/55617809. – dechiffre Nov 05 '21 at 13:41

12 Answers12

35

Try installing

pip3 install opencv-python==4.1.2.30  
kaizen
  • 1,132
  • 1
  • 14
  • 20
31

For Ubuntu users,

sudo apt-get install qt5-default fixes the issue.

(I'm using OpenCV 4.4)

WhaSukGO
  • 575
  • 7
  • 17
19

For me, it worked by using a opencv-python version prior to 4.2 version that just got released. The new version (4.2.0.32) released on Feb 2, 2020 seems to have caused this breaking change and probably expects to find Qt at a specific location (Users/ directory) as pointed by other answers.

You can try either manually installed from qt.io as suggested and making sure you get a .qt directory under yours Users directory, or you can use version 4.1.2.30, which works like charm without doing anything else.

It works for opencv-contrib-python too.

Simran Singh
  • 206
  • 1
  • 2
9

This can be solved installing python-opencv-headless instead of python-opencv

Nicochidt
  • 124
  • 2
  • 4
    It's `opencv-python-headless` – Rubén Salas Apr 02 '20 at 10:53
  • 3
    it does not work and gives me this error: `cv2.error: OpenCV(4.5.2) /tmp/pip-req-build-5wrl9sz4/opencv/modules/highgui/src/window.cpp:679: error: (-2:Unspecified error) The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Cocoa support. If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script in function 'cvShowImage'` – Franva May 17 '21 at 06:44
  • 1
    @FranvaI I have gotten the exact same errror. Did you solve it? – akshay acharya Jun 02 '21 at 05:44
3

Same issue here. No answer, but it's appearing in a similar setup. I've tried throwing many solutions at it:

  • Installing QT from brew,
  • Reinstalling from: qt.io/download-qt-installer
  • Installing from pip (using virtual environments)
  • Explicitly setting changing the environment variables
    • QT_PLUGIN_PATH="/Users/halopend/.qt/5.14.1/clang_64/plugins/"
    • QT_QPA_PLATFORM_PLUGIN_PATH="/Users/halopend/.qt/5.14.1/clang_64/plugins/platforms/"

Sometimes the issue appeared to be opencv having qt included within it which classed with an externally defined qt, but I'm not sure.

Anyway, not sure if that will help you, but at least you have a few ideas of where to look.

Jean Fradet
  • 104
  • 1
1

I met the same issue. I agree with Simran Singh. This issue comes from the recent update.

Quote from pacjin79 on Github:"If you are on a mac, make sure you install opencv-python-headless instead of opencv-python to avoid these errors." link

I personally solved the issue by doing so. Hope this works for you.

Victor Liu
  • 11
  • 1
1

Through many trial and error, for me it works for uninstalling and installing numpy and opencv.

1

Facing the same issue with PyQt5 and solving it by using PyQt6==6.3.1 and opencv-python==4.6.0.66.

1

I was facing the same issue. Turns out in my case, python 3.9 was causing this conflict. I managed to solve this by creating a new environment with python 3.8.

commands:

conda create -n myenv python=3.8.0
conda activate myenv
pip3 install opencv-python==4.2.0.34
lxhom
  • 650
  • 4
  • 15
1

In my case the solution from this link worked.

 export QT_QPA_PLATFORM=offscreen 
akbarnejad
  • 29
  • 2
  • It stopped to complain, but no window appears anywhere... Wait: doing back export QT_QPA_PLATFORM= starated to work – jaromrax Jul 14 '23 at 10:07
0

I ran into the same error after installing xrdp. The issue was solved after uninstalling xrdp and rebooting.

Amir
  • 1
0

OpenCv changes the environment variable 'QT_QPA_PLATFORM_PLUGIN_PATH' when you import it.

If you would change it back to what it should be using this:

import os
os.environ['QT_QPA_PLATFORM_PLUGIN_PATH'] = r'path/to/qt/plugins/platforms'

It would solve the issue.

You can get the path of your PyQt directory using the following command:

import os
import PyQt5
print(os.path.dirname(PyQt5.__file__))
Mehmet
  • 118
  • 7