Import error while importing cv2 in Spyder(Python 3.6)

不问归期 提交于 2021-02-08 06:08:32

问题


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?


回答1:


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!




回答2:


Open Anaconda prompt then type the command

pip install opencv-python

and this worked fine for me for anaconda 3.




回答3:


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



来源:https://stackoverflow.com/questions/51077327/import-error-while-importing-cv2-in-spyderpython-3-6

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!