'This application failed to start because no Qt platform plugin could be initialized

I am new to programming and 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 have tried googling the issue but either I couldn't implement the solutions right or they just do not work for me as most of them are for Windows' users.

I would appreciate any help with this issue. Thanks!



Solution 1:[1]

Try installing

pip3 install opencv-python==4.1.2.30  

Solution 2:[2]

For Ubuntu users,

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

(I'm using OpenCV 4.4)

Solution 3:[3]

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

Solution 4:[4]

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.

Solution 5:[5]

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

Solution 6:[6]

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.

Solution 7:[7]

This issue appears in the latest version of Opencv. I recommend downgrading your version to 4.1.2.30 or installing Qt in a specific directory.

pip install opencv-python==4.1.2.30  

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 khaidem
Solution 2 WhaSukGO
Solution 3 Nicochidt
Solution 4 Jean Fradet
Solution 5 Calvin Khoo
Solution 6 Victor Liu
Solution 7 KMM