Install OpenCV for Python on Ubuntu, get ImportError: no module named cv2.cv

I have a Ubuntu 14.04 system on which I want to install OpenCV and use it with Python 2.x.

I installed OpenCV using the following instructions: https://help.ubuntu.com/community/OpenCV

The installation seemed to work correctly, no errors, the script ended up exiting

OpenCV 2.4.9 ready to be used 

When I try to run a sample Python script, I get the following:

 $ python opencv.py Traceback (most recent call last): File "opencv.py", line 1, in <module> from cv2.cv import * ImportError: No module named cv2.cv 

I suspect I know why, I just don't know how to fix it. OpenCV is installed in the current directory that I was in when I ran the installation script, this is a subdirectory of my home folder.

Others that get this import error after installation seem to have a problem with the path, and they are fortunate enough to add this to their code:

 import sys sys.path.append('/usr/local/lib/python2.7/site-packages') 

or update their PYTHONPATH using the same directory. I tried to add this code, it does not matter. I do not see any files in the "site-packages" directory. Should I do the installation in this directory? I assume that the installation instructions would have to spell it out. I suspect my problem is that Python cannot find the OpenCV installation, but I'm not sure how to do this.

Please help me get a convenient installation of OpenCV as simple as possible.

+86
python ubuntu opencv importerror
Aug 09 '14 at 4:01
source share
17 answers

I think you do not have the python-opencv package.

I had exactly the same problem and

 sudo apt-get install python-opencv 

solved the problem for me.

You can install opencv at the following link https://www.learnopencv.com/install-opencv3-on-ubuntu/ This works for me. apt-get install does not contain many opencv packages

+109
Mar 24 '15 at 11:54
source share

I also had this problem. Tried different things. But finally

 conda install opencv 

worked for me.

+30
Dec 06 '15 at 17:46
source share

If you want to install from the repository as simple as possible:

 sudo apt-get install python-opencv libopencv-dev python-numpy python-dev 
+16
Feb 18 '15 at 14:45
source share

Use pip:

https://pypi.python.org/pypi/pip

 $ pip install SomePackage [...] Successfully installed SomePackage 

And when you add the path to PYTHONPATH with sys, PYTHONPATH always restarts to its default values โ€‹โ€‹when you close the Python shell. Check out this thread:

Constantly add directory to PYTHONPATH

First add openCV to your path (short guide):

https://help.ubuntu.com/community/OpenCV

after that install packages other than python, pyopencv depends on:

 sudo apt-get build-dep python-opencv 

finally use pip:

 pip install pyopencv 

Alternatively, you can check out this tutorial to install openCV on ubuntu 14.04 LTS

http://www.samontab.com/web/2014/06/installing-opencv-2-4-9-in-ubuntu-14-04-lts/

+11
Aug 09 '14 at 5:58
source share

Try conda install -c conda-forge opencv if you use anaconda it works!

+10
Jun 30 '16 at 13:50
source share

Find where cv2.so , for example /usr/local/lib/python2.7/dist-packages , then add it to your ~/.bashrc by doing:

 sudo gedit ~/.bashrc 

and add

 export PYTHONPATH=/usr/local/lib/python2.7/dist-packages:$PYTHONPATH 

In the last line

And then do not forget to open another terminal, it may be work, and I solved my problem. Hope this helps you.

+7
Jan 06 '16 at 8:44
source share

Make sure cv2.so has compiled: / usr / local / lib / python 2.7 / site-packages Then export this path like this

 export PYTHONPATH=/usr/local/lib/python2.7/site-packages:$PYTHONPATH 

Same as answer here

+5
Aug 27 '15 at 20:22
source share

My environment:

  • Ubuntu 15.10
  • Python 3.5

Since none of the previous answers worked for me, I downloaded OpenCV 3.0 from http://opencv.org/downloads.html and followed the installation guide . I used the following cmake command:

 $ ~/Programs/opencv-3.0.0$ cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local -D PYTHON3_EXECUTABLE=/usr/bin/python3.5 -D PYTHON_INCLUDE_DIR=/usr/include/python3.5 -D PYTHON_INCLUDE_DIR2=/usr/include/x86_64-linux-gnu/python3.5m -D PYTHON_LIBRARY=/usr/lib/x86_64-linux-gnu/libpython3.5m.so -D PYTHON3_NUMPY_INCLUDE_DIRS=/usr/lib/python3/dist-packages/numpy/core/include/ -D PYTHON3_PACKAGES_PATH=/usr/lib/python3/dist-packages .. 

Each step of the textbook is important. In particular, be sure to call sudo make install .

+4
Nov 27 '15 at 10:12
source share

I found the solution in the manual here:

http://www.samontab.com/web/2014/06/installing-opencv-2-4-9-in-ubuntu-14-04-lts/

I resorted to compiling and installing from source code. The process was very smooth, if I knew, I would start from this, instead of trying to find an easier way to install. Hope this information helps someone.

+3
Aug 10 '14 at
source share

Create a symlink to OpenCV. For example:

 cd ~/.virtualenvs/cv/lib/python2.7/site-packages/ ln -s /usr/local/lib/python2.7/dist-packages/cv2.so cv2.so ln -s /usr/local/lib/python2.7/dist-packages/cv.py cv.py 
+3
Mar 08 '16 at 11:06
source share

If you are really sure that you installed cv2, but it does not give module errors. There is a solution for this. Perhaps you have a cv2.so file in your directory

 /usr/local/lib/python2.7/site-packages/cv2.so 

move this cv2.so file to

 /usr/lib/python2.7/site-packages 

copy the file to the package sites directory

+2
Apr 14 '16 at 6:28
source share

if you use the pycharm platform, itโ€™s very easy to go to view => tool window ==> the python console, after that you will see the console below with [1]: type !pip install opencv-python

+1
Mar 18 '17 at 22:10
source share

Try using: from cv2 import cv

It works for me.

+1
05 Oct '18 at 7:05
source share

For those who are trying to use 3.1.0, but after installing python they say "cv2 module not found."

Most likely you have python but no python-dev.

 sudo apt-get install python-dev 

then reinstall 3.1.0 and it will work.

0
Sep 08 '16 at 20:08 on
source share

It seemed to me that this works on Max OSX: https://anaconda.org/menpo/opencv3

 conda install -c menpo opencv3=3.1.0 

I confirmed that you can import cv2 into python using python2.7 and python3

0
Nov 20 '16 at 22:59
source share

For me, this problem arose because I did not properly link the cv2.so file in the ~/.virtualenvs/cv/lib/python3.5/site-packages folder (your virualenv name cannot be "cv", your python version may not be 3.5 - adjust accordingly).

If you go to the ~/.virtualenvs/cv/lib/python3.5/site-packages and ls folder, the cv2.so file should appear in blue (Ubuntu 16.04), showing that it is connected. You can check the location of the link by typing: readlink cv2.so

If cv2.so is displayed in red (like mine), rm file and type: (for my python 3.5 installation)

 ln -s /usr/local/lib/python3.5/dist-packages/cv2.cpython-35m-x86_64-linux-gnu.so cv2.so 

OR (if you have python 3.6)

 ln -s /usr/local/lib/python3.6/dist-packages/cv2.cpython-36m-x86_64-linux-gnu.so cv2.so 

If you are running python 2.6 or python 2.7, enter instead:

 ln -s /usr/local/lib/python2.7/dist-packages/cv2.so cv2.so 

If the files cv2.so or cv2.cpython-36m-x86_64-linux-gnu.so do not exist at your location /usr/local/lib/python***/dist-packages , check to see if they are in the /usr/local/lib/python***/sites-packages folder /usr/local/lib/python***/sites-packages . If so, adjust the path accordingly. If not, something went wrong with your opencv installation.

This answer was inspired by the information here: https://www.pyimagesearch.com/2016/10/24/ubuntu-16-04-how-to-install-opencv/

0
Nov 09 '17 at 20:55
source share

This is a complete installation nightmare, but I will give one more hope that you can avoid building opencv from the source code:

pip install opencv-contrib-python

0
May 08 '19 at 17:55
source share



All Articles