89

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

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

The install seemed to run properly, no errors, the script ended with output

OpenCV 2.4.9 ready to be used

When I try to run the 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 installed to the current directory I was in when I ran the install script, it's a subdirectory of my home folder.

Others who get this import error after install seem to be having a path issue, and have luck adding this to their code:

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

or updating their PYTHONPATH with that same directory. I tried adding that code, it doesn't make a difference. I don't see any files in the "site-packages" directory. Should I have done the install in that directory? I imagine the installation instructions would have spelled that out. I suspect that my problem has to do with Python not finding the OpenCV install, but I'm not sure how to proceed.

Please help me get a usable install of OpenCV as simply as possible.

4
  • 1
    possible duplicate of OpenCV - cannot find module cv2
    – krock
    Aug 9, 2014 at 4:10
  • @krock I don't think this is a dup of that. I don't have the file cv2.so present in my site-packages directory like that user had. Also, I mentioned the two solutions that are given in that post, and neither worked for me here.
    – robm
    Aug 9, 2014 at 4:39
  • 1
    but you can import cv2 ? the old cv (or cv2.cv) module won't be there in upcoming opencv versions
    – berak
    Aug 9, 2014 at 6:18
  • @berak No, I can not import cv2 either.
    – robm
    Aug 10, 2014 at 18:57

23 Answers 23

109

I think you don't have the python-opencv package.

I had the exact same problem and

sudo apt-get install python-opencv

solved the issue for me.

you can install opencv from the following link https://www.learnopencv.com/install-opencv3-on-ubuntu/ It works for me . apt-get install doesnt contain many packages of opencv

6
  • Why your answer should be different from the others? Mar 24, 2015 at 12:58
  • The OP does have the package. Otherwise the ImportError would say, "no package named cv2". Aug 13, 2015 at 5:26
  • Same problem, tried installing per the instructions here: help.ubuntu.com/community/OpenCV No luck. This solution worked for me. Thanks!
    – jschabs
    Sep 3, 2015 at 13:22
  • 2
    That was so simple! After agonizing over so many other incredibly complex "solutions", none of which worked, this did the trick. Thanks! :-) Nov 4, 2015 at 2:20
  • sudo dnf install opencv-python for fedora. Apr 21, 2017 at 9:30
30

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

conda install opencv

worked for me.

2
  • 1
    This will work assuming that your python is Anaconda. Nov 11, 2016 at 18:19
  • 4
    Note conda install opencv dosen't work for windows 64 bit.
    – shahar_m
    Jan 15, 2017 at 13:38
17

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

sudo apt-get install python-opencv libopencv-dev python-numpy python-dev
2
  • 1
    I had to cut out the opencv-dev part, but otherwise, this answer worked nicely for me.
    – Brian Z
    Mar 28, 2015 at 7:45
  • Best answer here! Sep 5, 2018 at 20:31
11

Use pip:

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

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

And when you add a path to PYTHONPATH with sys, PYTHONPATH it's always restarted to default values when you close your Python shell. Check this thread:

Permanently add a directory to PYTHONPATH

First add openCV to your path (Quick guide):

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

after that, install the non-python packages pyopencv depends on:

sudo apt-get build-dep python-opencv

finally, use pip:

pip install pyopencv

Also, you can check this tutorial to install openCV in ubuntu 14.04 LTS

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

5
  • 4
    Which pip package should I use for OpenCV? There are many listed at pypi.python.org/pypi, not clear which if any is official and/or current.
    – robm
    Aug 10, 2014 at 18:56
  • I'm going to edit my answer with a short guide, please mark it as correct if was useful
    – Juan David
    Aug 11, 2014 at 12:50
  • 2
    This is not correct. The reason this error is shown is because the cv submodule is no longer present in recent versions. Aug 13, 2015 at 5:27
  • I'm not talking about the reason of the error. I'm just giving a possible solution. It works for me, maybe could be useful for someone else @AndyHayden
    – Juan David
    Aug 14, 2015 at 15:33
  • ERROR: Could not find a version that satisfies the requirement pyopencv (from versions: 2.0.wr1.0.1-demo, 2.0.wr1.0.1, 2.0.wr1.1.0, 2.1.0.wr1.0.0, 2.1.0.wr1.0.1, 2.1.0.wr1.0.2, 2.1.0.wr1.1.0, 2.1.0.wr1.2.0) ERROR: No matching distribution found for pyopencv May 8, 2019 at 17:46
11

Try conda install -c conda-forge opencv if you are using anaconda, it works!

9

Find where the cv2.so is, for example /usr/local/lib/python2.7/dist-packages, then add this into 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 remember to open another terminal, this can be work, and I have solve my problem. Hope it can help you.

5

Verify if cv2.so did compile, should be placed in: /usr/local/lib/python2.7/site-packages Then export that path like this

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

Same as in the answer here

4

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 manual. 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 tutorial is important. Particularly, don't forget to call sudo make install.

3

I found a solution in the guide 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. The process was very smooth, had I known, I would have started with that instead of trying to find a more simple way to install. Hopefully this information is helpful to someone.

3

Create a symbolic link to OpenCV. Eg:

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

Its complete installation nightmare, but I'll give one more hope you can avoid building opencv from source:

pip install opencv-contrib-python

3

You can build for source following the official OpenCV tutorial. The crucial part is to set the PYTHON3_EXECUTABLE, PYTHON_LIBRARY, PYTHON3_PACKAGES_PATH and PYTHON3_NUMPY_INCLUDE_DIRS parameters for python3.6. Here are all the steps:

  1. Clone the repo

    git clone https://github.com/opencv/opencv.git
    
  2. Create build directory

    cd ~/opencv
    mkdir build
    cd build
    
  3. Configure

    cmake -D CMAKE_BUILD_TYPE=RELEASE \
          -D CMAKE_INSTALL_PREFIX=/usr/local .. \
          -D PYTHON_INCLUDE_DIR=/usr/include/python3.6 \
          -D PYTHON_INCLUDE_DIR2=/usr/include/x86_64-linux-gnu/python3.6m \
          -D BUILD_NEW_PYTHON_SUPPORT=ON \
          -D BUILD_opencv_python3=ON \
          -D HAVE_opencv_python3=ON \
          -D INSTALL_PYTHON_EXAMPLES=ON \
          -D PYTHON3_EXECUTABLE=/usr/bin/python3.6 \
          -D PYTHON_DEFAULT_EXECUTABLE=/usr/bin/python3.6 \
          -D PYTHON_LIBRARY=/usr/lib/x86_64-linux-gnu/libpython3.6m.so \
          -D PYTHON3_PACKAGES_PATH=/usr/lib/python3/dist-packages .. \
          -D PYTHON3_NUMPY_INCLUDE_DIRS=/home/user/.local/lib/python3.6/site-packages/numpy/core/include/
    
  4. Build

    make -j8
    
  5. Install libraries

    sudo make install
    
  6. Test

    python3
    import cv2
    

If you don't get the error "No module named cv2", then the installation was successful.

Note: If you don't know the path to numpy for the PYTHON3_NUMPY_INCLUDE_DIRS parameter, you can find it by executing import numpy and then numpy.__file__ in a python3 shell.

2

If you really sure that you installed cv2 but it gives no module error. There is a solution for this. Probably you have 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 into site-packages directory

1

if you are using pycharm platform it's very simple go into view=>tool windows==>python console after that you will see in the bottom the console with [1] : type this !pip install opencv-python

1

Try using: from cv2 import cv

It works for me.

1

I tried all the other options here, but I could not get import cv2 working with Anaconda on Ubuntu. This is the only thing that helped:

pip install opencv-python

1
  • thank you. it working : pip3 install opencv-python. ////// Testing: python3 / import numpy / import cv2 Jun 24, 2020 at 5:08
0

This seemed to work for me on Max OSX: https://anaconda.org/menpo/opencv3

conda install -c menpo opencv3=3.1.0

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

0

For me, this problem was due to the fact that I had not appropriately sym-linked the cv2.so file in the~/.virtualenvs/cv/lib/python3.5/site-packages folder (the name of your virualenv may not be "cv", your version of python may not be 3.5--adjust accordingly).

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

If cv2.so appears in red (as mine did), rm the file and type: (for my install of python 3.5)

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 working in python 2.6 or python 2.7, you instead type:

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

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

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

0

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

You likely have python but not python-dev.

sudo apt-get install python-dev

then reinstall 3.1.0 and it'll work.

0

try using sudo apt install python3-opencv

it will install the latest package of open cv.

Or you could try reinstalling the opencv package. It might have got corrupted during installation.

0

Uninstall pandas, then install it again:

pip uninstall pandas
pip install pandas
0

This solved my problem:

sudo apt-get install python3

then

pip3 install opencv-python
0

The error kept appearing to me after downloading openCV 5 times in a different ways, until i fond that all of them has downloaded in a random version of Python, all of them in ~/usr/local/lib/python3.10 and the latest version is 3.11 (which i expected that the terminal should be using it when i write 'python3').

that was the problem in my case, i just changed python version in vscode.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.