ImportError: No module named cv2

25,199

Another reason might be a missing OpenCV module. On my Mac OSX El Capitan [10.11.2 (15C50)], I had the exact same error with Anaconda install, and this resolved the issue:

conda install opencv

While that helped deal with:

ImportError: No module named cv2

It also introduced the following issue:

ImportError: numpy.core.multiarray failed to import

because somehow the numpy version got switched back to 1.7.0. So performing this, worked:

conda update numpy

Double check:

import numpy
print numpy.__version__
1.10.2

Now all good.

Share:
25,199
Alwin Hui
Author by

Alwin Hui

Updated on November 01, 2020

Comments

  • Alwin Hui
    Alwin Hui over 3 years

    I've been trying to figure this error out for the past day and I've looked up error messages all over the internet and still can't figure out how to get past this error.

    I have OpenCV and cv2 setup on my desktop, but I need to program on my laptop now (for mobile reasons). Unfortunately, even though I have OpenCV downloaded, when I try to import cv2, it gives me the error message, "ImportError: No module named cv2".

    The closest I've gotten so far is "locate cv2" which gives me "/usr/lib/python2.7/dist-packages/cv2.so". I then go into the python shell and run "import sys" then "sys.path.append('/usr/lib/python2.7/dist-packages')" which then gives me a True when I ask "'/usr/lib/python2.7/dist-packages' in sys.path". However, when I then try to import cv2, it now returns a new error message of "ImportError: numpy.core.multiarray failed to import". I tried resolving this error, but I had no luck on this either.

    I've tried everything on forums and message boards online and can't figure out how to fix this. ANY help would be extremely appreciated, as I need to complete this program by the end of the week.

  • Alwin Hui
    Alwin Hui about 9 years
    I have already tried 2 and 3. 2 returns "Requirement already satisfied (use --upgrade to upgrade): numpy in /usr/lib/python2.7/dist-packages" 3 returns "0 upgraded, 0 newly installed, 0 to remove and 10 not upgraded." Based on that, I presume that this means that numpy is already installed system-wide. I could try the virtualenv, but I prefer to have it system-wide.
  • Velimir Mlaker
    Velimir Mlaker about 9 years
    It sounds like your NumPy version is outdated. Try doing what it says: For 2) try pip install --upgrade numpy, and for 3) try apt-get update; apt-get upgrade.
  • Alwin Hui
    Alwin Hui about 9 years
    I just did your two suggestions for 2 and 3. Now, when I try to import numpy in the python shell, it returns "Import Error: /usr/lib/python2.7/dist-packages/numpy/core/multiarray.so: undefined symbol: PyUnicodeUCS4_AsUnicodeEscapeString".
  • Velimir Mlaker
    Velimir Mlaker about 9 years
    It sounds like you got different versions of numpy mixed together, the one installed with pip and the other with apt-get. Let's try fixing this by just focusing on method #3 and forget method #2. So, first remove the one you installed with pip: pip uninstall numpy and then try importing the module from python shell.
  • Alwin Hui
    Alwin Hui about 9 years
    After doing "pip uninstall numpy" and uninstalling numpy, I then ran "import numpy" on the Python shell. It now says, "ImportError: no module named numpy."
  • Alwin Hui
    Alwin Hui about 9 years
    I tried that and it didn't work. However, I uninstalled Ubuntu and did a clean re-install, following only these instructions and it worked.
  • Alwin Hui
    Alwin Hui about 9 years
    For people who are here looking for a solution, the way I was able to solve it was by installing Ubuntu again, and following these instructions: samontab.com/web/2014/06/…. Probably not the most efficient way of solving the problem, but it worked. Good luck!