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

Question:

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!

Asked By: Nick Foley

||

Answers:

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.

Answered By: Jean Fradet

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.

Answered By: Simran Singh

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.

Answered By: Victor Liu

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

Answered By: Nicochidt

Try installing

pip3 install opencv-python==4.1.2.30  
Answered By: kaizen

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

Answered By: Calvin Khoo

For Ubuntu users,

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

(I’m using OpenCV 4.4)

Answered By: WhaSukGO

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

Answered By: Benyamin Zojaji

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

Answered By: Amir

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
Answered By: Kaies Al Mahmud

In my case the solution from
this link
worked.

 export QT_QPA_PLATFORM=offscreen 
Answered By: akbarnejad

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__))
Answered By: Mehmet

the command below works for me, when I installed freesurfer and failed to launch it. ubuntu 22.04

sudo apt install qtbase5-dev qtchooser qt5-qmake qtbase5-dev-tools
Answered By: cy l