# Import Error: cannot import name 'cv2'

I want to begin exploring OpenCV in Python but I'm stuck at importing the package cv2. I have installed the package through pip3 install opencv-python and it got installed at this location - C:/Users/Kshitiz/AppData/Local/Programs/Python/Python36-32/Lib/site-packages.

When I'm trying to import cv2 using this:

import sys
sys.path.append('C:/Users/Kshitiz/AppData/Local/Programs/Python/Python36-32/Lib/site-packages')
import cv2


It gives the following error:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:/Users/Kshitiz/AppData/Local/Programs/Python/Python36-32/Lib/site-packages\cv2\__init__.py", line 7, in <modul
e>
from . import cv2
ImportError: cannot import name 'cv2'


I have searched a lot but cannot find anything relevant. Please suggest what needs to be done.

• Check if this link helps : stackoverflow.com/questions/19876079/…
– Indi
Jul 11 '17 at 7:54
• Does the site-packages folder contain cv2.pyd file? This binding is required to import cv2 in python. Jul 12 '17 at 17:09
• @Gingerbread Yes there's a cv2.cp36-win32.py file. Jul 12 '17 at 17:28
• try this one"pip install --upgrade pip" Dec 3 '18 at 7:15
• Please make your answer more detailed. Your answer might be correct, but it will be easily ignored if does not contain proper explanation. Refer this and try to elaborate on your answer well. Dec 3 '18 at 7:55

According to the official documentation, if you had previous versions of opencv-python and opencv-contrib-python installed, you should consider removing them first:

 pip uninstall opencv-python
opencv-contrib-python


Also, can you try installing them as sudo?

sudo -H pip install opencv-python

• Doing pip uninstall opencv-python gives me a message that open-cv is not installed in my computer. I'm using windows os. Jul 12 '17 at 17:32

Although you'll find a lot of tutorials that help you install opencv and ffmpeg (both go hand in hand almost always) out there, I will post what worked for me here.

Steps for installing ffmpeg correctly

2. Extract the installed zip file using 7-zip. The folder will be named something like "ffmpeg-20130731-git-69fe25c-win32-static".
3. Create a new folder named ffmpeg in your C: and copy the contents of the extracted folder into this new one.
4. Now click the start button, right-click on computer. Select Properties from the right-click menu. In the System window, click on the “Advanced system settings” link.
5. Click the Environmental Variables button in the System Properties window. It will be located at the bottom of the window.
6. Select the PATH entry in the "User variables" section. Click the Edit button. In the “Variable value” field, enter ;c:\ffmpeg\bin after anything that's already written there. If you copied it to a different drive, change the drive letter. Click OK to save your changes. If there is no PATH entry in the "User variables" setting, click the New button and create one. Enter PATH for the variable name. This method will enable FFmpeg for the current user. Other Windows users will not be able to run it from the command line. To enable it for everyone, enter ;c:\ffmpeg\bin in the PATH entry in "System variables". Be very careful not to delete anything that is already in this variable.
7. Open the command prompt. Enter the command ffmpeg –version. If the command prompt returns the version information for FFmpeg, then the installation was successful, and FFmpeg can be accessed from any folder in the command prompt. If you receive a libstdc++ -6 is missing error, you may need to install the Microsoft Visual C++ Redistributable Package, which is available for free from Microsoft.

I followed these steps from a site I don't remember but this worked just as fine.

Now to install opencv and get it working, I followed the following steps:

2. Run the Executable which will in turn extract the executable.
3. Create a folder in C:\ called opencv
4. Copy the contents of the extracted files into opencv
5. Edit your PATH environment variable. This will be dependent on the version you want installed (i.e. 32 or 64 bit) and also the version of visual studio you have installed (express editions are fine).

----Installing OpenCV Python I use Anaconda. So, I copied the cv2.pyd file from this OpenCV directory (the beginning part might be slightly different on your machine):

# Python 2.7 and 32-bit machine:

C:\opencv\build\python\2.7\x84

# Python 2.7 and 64-bit machine:

C:\opencv\build\python\2.7\x64

To this Anaconda directory (the beginning part might be slightly different on your machine):

C:\Users\xxx\Anaconda\Lib\site-packages

OpenCV also requires that numpy be installed as well. matplolib is also recommended to be installed.

You'll also have to locate the two .dll files in the bin folder of your opencv folder. And copy and paste them into the ffmpeg/bin folder as well as into the site-packages folder in your Anaconda folder. When I installed opencv, I didn't have the bin folder installed (weird, I know). So, I copied my friend's .dll files and renamed them to the version of opencv that I was using and it worked!

After installing both OpenCV and ffmpeg you'll need to restart your computer to add the paths to the system path.

Hope this helps! For a lot of people, most of the steps above were not necessary. But for me, this worked! I use Windows 10, Opencv 3.0, Python 2.7!

If you are using conda package manager, it's very straightforward to install opencv by executing:

conda install -c menpo opencv3

• Doing conda install opencv gives me No Package Error. Doing what you said gives me - UnsatisfiableError: The following specifications were found to be in conflict: - opencv3 -> python 2.7* - python 3.6* Use "conda info <package>" to see the dependencies for each package. Jul 12 '17 at 17:37

I had the same issue when i used opencv>=3.4 , gives DLL Load Failed error when i used python opencv-contrib-python in windows 32 bit in Anaconda3 32 bit installer.

I used API-MS-WIN-DOWNLEVEL-SHLWAPI-L1-1-0.DLL file and kept it in windows/system32/ folder. Check this file is listed there or not. If there is DLL Load error , this file must be missing.