Import error while importing cv2 in Spyder(Python 3.6)

Question:

I have already installed opencv 3.0.0 in windows os. I have run the application and have successfully installed it in C: drive and have also copied the cv2.pyd file in C:Python27Libsite-packages as I saw in several tutorial videos and
in my Python 2.7.5 Shell i.e IDLE when I type

>>>import cv2

>>>

It shows it has been safely installed and running.

Now I am using Spyder 3.6 as this is the latest version available for my Machine Learning programs but here if I follow the above steps it shows the error

ModuleNotFoundError: No module named ‘cv2’

Now I copy the same cv2.pyd file in the path

C:UsersChirantanDocumentsWinPythonpython-3.6.5.amd64Libsite-packages

But the error now is

File “ipython-input-36-c8ec22b3e787”, line 1, in

import cv2

ImportError: DLL load failed: %1 is not a valid Win32 application.

I have 2 questions do I have to install a separate opencv for Spyder 3.6 and install it in C:UsersChirantanDocumentsWinPython

and copy the cv2.pyd file in C:UsersChirantanDocumentsWinPythonpython-3.6.5.amd64Libsite-packages

In this case it shows an error

File “ipython-input-36-c8ec22b3e787 “, line 1, in

import cv2

ImportError: DLL load failed: %1 is not a valid Win32 application.

How do I solve this problem?

What should be the correct version of opencv to be installed for Spyder 3.6 and where should be the location where it should be saved and ehere should I put the cv2.pyd file?

Any Help Is Appreciated.

No I want to know what should be the correct version of opencv to be installed for Python 3.6 and provided I have an updated numpy that comes with the spyder package i.e winpython package, where should I install and save the correct file opencv in the winpython folder?

Asked By: Saradamani

||

Answers:

Go to https://pypi.org/project/opencv-python/

and copy pip install opencv-python

Now open C:UsersXYZ...WinPython folder and open WinPython command prompt which is inside the folder WinPython

Here paste pip install opencv-python and press enter

Next type pip install opencv-contrib-python and press enter.

Voila! Your opencv is installed. Open your Python 3.6.5 Shell and type

>>> import cv2

>>>

It is successfully installed!

Answered By: Saradamani

It worked well for me. Answer is need to put cv2.pyd file to your virtual environment. Need to put under two folder of envs,

First is under .dll folder and second is under Lib/site-packages

To get cv2.pyd

Download from this link enter link description here and then extract the download file

You will get opencv folder, after that go inside opencv folder like

Downloadsopencvbuildpython2.7
and go one hierarchy deeper in your folder structure depending on your your system architecture; 32- or 64- window version

Copy cv2.pyd
Important: After copying cv2.pyd file to your envs, you need to rename cv2.pyd to _cv2.pyd

Answered By: kaythi swetun

Open Anaconda prompt then type the command

pip install opencv-python

and this worked fine for me for anaconda 3.

Answered By: Fardad Ansari

I have found this working for me.
I am using windows10, python 11 and spyder 5.4
It works if you DON’T install opencv-python with pip install, but use the one coming by default in python and spyder!

Answered By: SylvainK
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.