7

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
  • 225
  • 2
  • 3
  • 7

3 Answers3

7

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.

Kingz
  • 5,086
  • 3
  • 36
  • 25
0

I am currently working with Google VM (ubuntu 14.04). Installing opencv on python3.4 version has been quite a task. I wanted opencv to be installed for python 3.4 but everytime it was getting installed on 2.7 version.

I will share the steps I followed so as to help others on it.

Step 1 Follow all the steps as mentioned on openCv installation part till cmake. Link is given below: https://docs.opencv.org/master/d7/d9f/tutorial_linux_install.html

Note: Install all the 3 packages mentioned at start. That optional one too..!! And dont forget to change the python version for which you are installing.

I did

sudo apt-get install python3-dev python3-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev

Follow step 2 for cmake.

Step 2 For installing opencv in specific version of python (ubuntu), you have to set the default (PYTHON_DEFAULT_EXECUTABLE) with the path to where your python is installed. You can find that out by using command whereis python3.4 (or, your version). Mine was in /usr/bin/python3.4

Instead of cmake mentioned on the page, use this,

cmake -D CMAKE_BUILD_TYPE=Release -D BUILD_NEW_PYTHON_SUPPORT=ON -D BUILD_opencv_python3=ON -D HAVE_opencv_python3=ON -D PYTHON_DEFAULT_EXECUTABLE=/usr/bin/python3.4 ..

Note: Dont forget to change your python version and path in PYTHON_DEFAULT_EXECUTABLE.

Step 3 Follow the remaining steps as mentioned in the link till sudo make install

Hope it helps.

Vaibhav
  • 71
  • 1
  • 3
-2

You're missing NumPy, which can be installed one of several ways. Here's some possibilities, listed in order of (my personal) preference:

1) Inside a virtualenv. See https://stackoverflow.com/a/19213369/1510289 on how to do this.

2) System-wide, if you have pip installed:

pip install numpy

3) System-wide, using your package manager. For example on Ubuntu:

apt-get install python-numpy

or on YUM systems, like Fedora:

yum install numpy
Community
  • 1
  • 1
Velimir Mlaker
  • 10,664
  • 4
  • 46
  • 58
  • 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. – Alwin Hui Mar 18 '15 at 18:16
  • 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``. – Velimir Mlaker Mar 18 '15 at 18:23
  • 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". – Alwin Hui Mar 18 '15 at 18:34
  • 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. – Velimir Mlaker Mar 18 '15 at 18:51
  • 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 Mar 19 '15 at 00:47
  • 1
    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 Mar 24 '15 at 18:52
  • 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: http://www.samontab.com/web/2014/06/installing-opencv-2-4-9-in-ubuntu-14-04-lts/. Probably not the most efficient way of solving the problem, but it worked. Good luck! – Alwin Hui Mar 24 '15 at 18:52