33

I have installed OpenCV 3.1 in my Mac, cv2 is also installed through pip install cv2.

vinllen@ $ pip install cv2
You are using pip version 7.1.0, however version 7.1.2 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
Requirement already satisfied (use --upgrade to upgrade): cv2 in /usr/local/lib/python2.7/site-packages

But it looks like cv2 and cv cannot be used:

Python 2.7.10 (default, Jul 13 2015, 12:05:58)
[GCC 4.2.1 Compatible Apple LLVM 6.1.0 (clang-602.0.53)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named cv2
>>> import cv
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named cv

I have tried almost all the solutions list online, but cannot work.

vinllen
  • 1,369
  • 2
  • 18
  • 36
  • There are many questions regarding this : http://stackoverflow.com/questions/15790501/why-cv2-so-missing-after-opencv-installed and http://answers.opencv.org/question/58626/opencv-with-python-on-a-mac/ and http://stackoverflow.com/questions/3325528/how-to-install-opencv-for-python and http://stackoverflow.com/questions/19876079/opencv-cannot-find-module-cv2 – udit043 Jan 18 '16 at 11:14

9 Answers9

136

You can install by

pip install opencv-python
Ráfagan
  • 2,165
  • 2
  • 15
  • 22
  • 10
    This should be the accepted answer. This just worked without having to go through the long and tedious tutorial from Imanol Luengo. – Frak Mar 01 '18 at 15:34
  • 3
    @frakman1 agreed! When I posted my answer OpenCV didnt have a `pip` install. As of today this is the correct way to go! – Imanol Luengo Mar 31 '18 at 19:41
  • 1
    You might also want to install `opencv-contrib-python` depending on your project requirements. Check https://docs.opencv.org/master/ for a list of extra modules. – MarcioPorto Sep 18 '18 at 19:52
  • 1
    OP should choose this one as the answer. Then others won't need to navigate down the page. – nad Feb 26 '20 at 19:14
  • 1
    this will only let you use your CPU as it does not include any of the CUDA bindings – FPcond Apr 09 '21 at 15:06
27

I do not know what pip install cv2 actually installs... but is surely not OpenCV. pip install cv2 actually installs this, which are some blog distribution utilities, not sure what it is, but it is not OpenCV.


To properly install OpenCV, check any of the links @udit043 added in the comment, or refer to any of the tutorials bellow:

Find here a tutorial on how to install OpenCV on OS X: http://www.pyimagesearch.com/2015/06/15/install-opencv-3-0-and-python-2-7-on-osx/

You need to actually compile OpenCV from source and activate python bindings, which takes a while.

Another option is to use brew to get OpenCV, but doesn't neccesarilly get you the last version nor a fully optimized one:

https://web.archive.org/web/20171218032016/http://www.mobileway.net/2015/02/14/install-opencv-for-python-on-mac-os-x/

Samuel Liew
  • 76,741
  • 107
  • 159
  • 260
Imanol Luengo
  • 15,366
  • 2
  • 49
  • 67
  • 1
    Thanks imaluengo, the url in the bottom helps me. 1.brew tap homebrew/science 2.brew install opencv 3. cp /usr/local/Cellar/opencv/2.4.12_2/lib/python2.7/site-packages/* /usr/local/lib/python2.7/site-packages/ where the step 3 can solve the problem of link error lists in the blog – vinllen Jan 18 '16 at 12:46
  • That site mobileway.net has been hacked – Nick Mitchell Nov 08 '16 at 05:20
  • 1
    @vinllen Thanks! After following the tutorial this step is necessary: `cp /usr/local/opt/opencv3/lib/python2.7/site-packages/cv2.so /usr/local/lib/python2.7/site-packages/` since it seems that the `opencv3.pth` file that points to the `/opt/opencv3` lib does not work. – loretoparisi Jan 18 '17 at 11:36
  • 5
    This is no longer the best answer. See below upvoted answer – SwimBikeRun Sep 04 '18 at 18:57
  • `Error: homebrew/science was deprecated. This tap is now empty and all its contents were either deleted or migrated.` – Dr.jacky Jan 16 '22 at 14:24
5

I used conda install opencv and it installed fine for me.

You might want to try this if you are using Anaconda.

Monica Heddneck
  • 2,973
  • 10
  • 55
  • 89
1

For me installation using conda didn't worked. While installing it using pip worked:

pip install opencv-python

os: macos Catalina

Saurabh Singh
  • 1,241
  • 13
  • 11
0

pip3 install opencv-python

Should do the job.

-1

Make sure that numpy, other dependency is installed prior installing OpenCV
Also if you installed using PIP then check the installed packages using

pip freeze
Vijay
  • 17
  • 3
-1

You can install by

conda install -c https://conda.binstar.org/menpo opencv
Alice
  • 178
  • 2
  • 9
-1

I had the same problem; here's what worked for me: conda install -c conda-forge nb_conda

If you haven't already, do the following to get conda up and running on OS X (taken from docs):

  1. Download Miniconda
  2. Download Anaconda
  3. Locate to the director that contains the Miniconda file and run bash Miniconda3-latest-MacOSX-x86_64.sh in Terminal
  4. Follow the prompts to install Anaconda
  5. Run conda install -c conda-forge nb_conda

You could also try conda install -c conda-forge opencv and conda install -c conda-forge/label/broken opencv if step 5 doesn't work, as someone recommended when I had the same problem. Hope this helps!

solo
  • 743
  • 2
  • 6
  • 17
-2

pip is renamed to pip3 so please use

pip3 install opencv-python

Xcoder
  • 33
  • 3
  • `pip` should point to `pip3` see discussion here: https://stackoverflow.com/questions/40832533/pip-or-pip3-to-install-packages-for-python-3 – Arthur Morris Oct 23 '20 at 05:39