7
votes

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.

3
what os? maybe you installed version for python 3 but you use python 2, or opposite?Marcin
is numpy installed on your laptop?Mailerdaimon
@Marcin: I have Ubuntu 14.04 running. When I do "python --version" it returns python 2.7.9. However, when I go into "/usr/lib/", I see both Python2.7, Python3, and Python3.4.Alwin Hui
@Mailerdaimon I believe so, but I have no idea how to check.Alwin Hui

3 Answers

7
votes

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.

0
votes

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.

-2
votes

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