Import error while importing cv2 in Spyder(Python 3.6)
Asked Answered
W

4

5

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:\Python27\Lib\site-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:\Users\Chirantan\Documents\WinPython\python-3.6.5.amd64\Lib\site-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:\Users\Chirantan\Documents\WinPython

and copy the cv2.pyd file in C:\Users\Chirantan\Documents\WinPython\python-3.6.5.amd64\Lib\site-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?

Wriggle answered 28/6, 2018 at 7:40 Comment(1)
W
3

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

and copy pip install opencv-python

Now open C:\Users\XYZ\...\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!

Wriggle answered 28/6, 2018 at 8:6 Comment(0)
P
5

Open Anaconda prompt then type the command

pip install opencv-python

and this worked fine for me for anaconda 3.

Plenty answered 5/8, 2019 at 13:49 Comment(1)
They're not using AnacondaMil
W
3

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

and copy pip install opencv-python

Now open C:\Users\XYZ\...\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!

Wriggle answered 28/6, 2018 at 8:6 Comment(0)
P
1

Edit the PYTHONPATH from the PYTHONPATH Manager in the Spyder Taskbar.

This is how PYTHONPATH Manager icon looks in the Spyder Taskbar.

Add the cv2 folder location from your installation there. I restarted the kernel in my case (don't know if it would work without the restart).

From then on, import cv2 worked fine.

11

Puerile answered 21/7 at 20:2 Comment(0)
V
0

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

Downloads\opencv\build\python\2.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

Vandiver answered 12/4, 2019 at 4:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.