20

I have Anaconda (version: conda 4.2.9, python3) installed and am trying to do import cv2 when I get the following error:

ImportError: No module named 'cv2'

With conda search cv2 I get this:

  opencv                     2.4.2                np15py26_0  defaults        
                             2.4.2                np15py27_0  defaults        
                             2.4.2                np16py26_0  defaults        
                             2.4.2                np16py27_0  defaults        
                             2.4.2                np17py26_0  defaults        
                             2.4.2                np17py27_0  defaults        
                             2.4.2                np15py26_1  defaults        
                             2.4.2                np15py27_1  defaults        
                             2.4.2                np16py26_1  defaults        
                             2.4.2                np16py27_1  defaults        
                             2.4.2                np17py26_1  defaults        
                             2.4.2                np17py27_1  defaults        
                             2.4.6                np16py26_0  defaults        
                             2.4.6                np16py27_0  defaults        
                             2.4.6                np17py26_0  defaults        
                             2.4.6                np17py27_0  defaults        
                             2.4.6                np18py26_0  defaults        
                             2.4.6                np18py27_0  defaults        
                             2.4.9                np18py27_0  defaults        
                             2.4.10               np19py26_0  defaults        
                             2.4.10               np19py27_0  defaults        
                             2.4.10              np110py27_1  defaults        
                             2.4.10               np19py26_1  defaults        
                             2.4.10               np19py27_1  defaults        

What do I need to do to be able to import the cv2 module?

I am using Ubuntu 16.04.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Linda
  • 1,003
  • 1
  • 11
  • 25

7 Answers7

23

opencv is not compatible with python 3. I had to install opencv3 for python 3. The marked answer in how could we install opencv on anaconda? explains how to install opencv(3) for anaconda:

Run the following command:

conda install -c https://conda.binstar.org/menpo opencv

I realized that opencv3 is also available now, run the following command:

conda install -c https://conda.binstar.org/menpo opencv3

Edit on Aug 18, 2016: You may like to add the "menpo" channel permanently by:

conda config --add channels menpo

And then opencv can be installed by:

conda install opencv (or opencv3)

Edit on Aug 14, 2017: "clinicalgraphics" channel provides relatively newer vtk version for very recent python3

conda install -c clinicalgraphics vtk

Edit on April 16, 2020 (based on @AMC's comment): OpenCV can be installed through conda-forge (details see here)

conda install -c conda-forge opencv

Community
  • 1
  • 1
Linda
  • 1,003
  • 1
  • 11
  • 25
  • 1
    _Edit on Mar 10, 2020: Hong's answer is the currently suggested way of installing opencv3 by Anaconda (see here) conda install -c menpo opencv=3_ That package is unmaintained since late 2017, possibly earlier. OpenCV is available from the main Anaconda channel and from conda-forge. – AMC Apr 16 '20 at 01:45
11

You can try

conda install -c menpo opencv=3
Hong
  • 526
  • 6
  • 21
  • @padd13ear That package is unmaintained since late 2017, possibly earlier. OpenCV is available from the main Anaconda channel and from conda-forge. – AMC Apr 16 '20 at 01:46
9

Use this code at the Anaconda prompt:

conda update conda -c conda-canary

Then don't type this code:

conda install -c conda-forge opencv

but, instead, use:

Pip install opencv-python

It worked with me after few days of trials

See Krishna Ojha's video and read the comments below the video.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Ama Mo
  • 91
  • 1
  • 2
  • Is this using anaconda with Python 3 or 2? Do you run the `pip` command from the Anaconda prompt? – alelom Jul 05 '20 at 15:25
  • Excellent answer, this worked for me 19/07/2020, you type all this in Anaconda prompt not cmd – homelessmathaddict Jul 19 '20 at 13:26
  • 4/9/2023: I cannot believe that 3 years later this still seems to be the fix: I spent a a day trying to install opencv with conda and finally gave up. Installing with pip from the conda prompt took less than a minute! – Wappler Apr 09 '23 at 18:29
5

I had created my Anaconda environment and installed everything I needed, but when trying to import cv2, I always got the message "no module named cv2". I reinstalled and followed Linda's tips, which didn't work.

Finally, I noticed that my Anaconda environment didn't have cv2 installed, even with all I did.
I copied the cv2 folder to "miniconda'X'/envs/yourEnv/lib/python'X.X'/site-packages/" which worked for me.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Paulo Ricardo
  • 69
  • 1
  • 3
3

The file may be not installed properly.

Run the following commands:

Sudo apt-get update
Sudo apt-get upgrade

then

pip install opencv-python

Check-in your jupyter notebook:

import cv2
print cv2.__version__

This may solve the issue.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Pronab Roy
  • 1,058
  • 1
  • 14
  • 18
0

If aforementioned solutions don't work for you & you have opencv installed in conda environment then try the following command:

conda install nb_conda
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
0

Using the Anaconda Graphical Interface.

  1. Launch Anaconda
  2. Click on Environment
  3. Select Target Environment
  4. Select "All" from the list of Options
  5. Search for "OpenCv"
  6. Install