Using OpenCV with Python on your Mac OS X

English: Python logo, Source: Wikipedia

You are likely to be sorry if you casually started installing Python and OpenCV like me as documented here. You might have read stuff online and tired installing python26 or python27 as well as numpy and opencv using macports hoping that your py scripts will conveniently import cv

Of course then you must have gotten stuck while running your python files which don’t find the cv modules. You might be clawing your forehead and face with your nails because of this infernal message:

ImportError: No module named cv

Do not worry! You can do these things to be on your way:

Step1A: Install a version of python

You should have xcode installed on your mac. You can get it from the app store. There is a version of python that ships with Mac which is generally called  the system python. Is is located in  /System/Library/Frameworks/Python.framework and /usr/bin/python

This is likely to be fairly old and you might want to instead get a more recent version from or use Macports. There is also a way to get it using Homebrew but I’d rather not get into that. Follow the prompts for prepackaged installer or use the following code from your terminal:

$ sudo port install python27

Step1B: If you have already installed stuff, check what is installed where

The prepackaged installers and Macports install python in certain different places.

  1. As noted earlier, system python is in /System/Library/Frameworks/Python.framework and /usr/bin/python
    • Indeed, you shouldn’t mess with these by trying to delete them manually. The system python could be in use by other softwares in your computer.
  2. Python installed using a .dmg is in /Library/Frameworks/Python.framework/Versions/2.7/bin/
    • You can manually remove the Python.framework folder. You might also see Python in the Applications folder along with other things like IDLE, AppBuilder and PythonLauncher. You can just go ahead and delete those if you need to.
  3. Python installed using Macports is in /opt/local/Library/Frameworks/Python.framework/Versions/2.7/bin/
    • You can uninstall them by using sudo port uninstall packageName and if you want to remove dependencies use something like this code:
      sudo port uninstall python27 --follow-dependents installed
    • All additional libraries like NumPy, SciPy, PIL, openCV, etc. that you try to install using port will go to /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages

I checked each one’s version by explicitly typing their location:

System python:

$ /usr/bin/python -c 'import sys;print(sys.version)' 
2.7.1 (r271:86832, Jun 16 2011, 16:59:05) 
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)]

Standard python using prepackaged installer from

$ /Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7 -c 'import sys;print(sys.version)' 
2.7.3 (v2.7.3:70274d53c1dd, Apr 9 2012, 20:32:06) 
[GCC 4.0.1 (Apple Inc. build 5493)]

Latest python using Macports:

$ /opt/local/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7 -c 'import sys;print(sys.version)' 
2.7.3 (default, Apr 19 2012, 00:55:09) 
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)]

Default python accessed by terminal:

$ python -c 'import sys;print(sys.version)' 
2.7.3 (default, Apr 19 2012, 00:55:09) 
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)]

The alias python2.7 also points to the same place as python

You can do the following things to make any version of python as default one your system however I opted to only use the version installed by Macports. This is explained bellow.

Step2: Direct your terminal to look for the particular python installation you need

From your terminal open your .bash_profile within TextEdit app:

$ open -e .bash_profile

Write this at the beginning of the file; this prepends to the PATH variable:

export PATH=/opt/local/Library/Frameworks/Python.framework/Versions/2.7/bin:$PATH

Putting $PATH at the end of folder location rather than beginning will prepend to the PATH variable. Doing something like export PATH=$PATH/folderLocation/:  would have appended it.

Then save the file and source it

$ source .bash_profile

From there onwards typing python in the terminal will open the very first one found by it which will be the version installed using Macports.

Step3: Install NumPy and OpenCV

You can see which port versions of a group are available by typing

$ sudo port select --list python

A bunch of versions of python were available to me but I need python27 to be used as default with my python packages so I made that one active:

$ sudo port select --set python python27

OpenCV requires NumPy so I installed the two using Macports like so

$ sudo port install py27-numpy 
$ sudo port install opencv +python27 

Now you are ready to start using OpenCV with python.

Eventually you might need to install a newer version of these packages or even a new version of python. The packages can be updated like this

$ sudo port upgrade installed

For more information on using port and installing or uninstalling packages you can open its man pages:

$ man 1 port

You can then install Distribute, PIP and VirtualEnv for convenience.

14 thoughts on “Using OpenCV with Python on your Mac OS X

  1. tried this multiple times; didn’t work for me. still getting “no module named cv”. slowly losing sanity.

    also install numpy should be install py27-numpy.

    1. hi,

      thank you for pointing out the correction for “install py27-numpy” i’ve made the change. do have a look at the article “my misery in making opencv 2.4 work with python 2.7 mac os x lion”. start reading from the heading: “my way out of my mess”

      to diagnose the problem you should definitely look at “which python” by using the methods i’ve described in step1b in this article.

  2. Hello Sam,

    I followed all of these steps, but I still cannot get it to work. When I try to import cv, it says ImportError: No module named cv.

    What am I doing wrong here? Could you please point me to the right direction.

    Thank you.

    1. hi Hussain,

      could you tell me about your OS version. If you have upgraded Mavericks did you reinstall X Code? if you have Mavericks please install the developer version of X Code. Please tell me about your ‘PATH’ variable i.e. type `echo PATH` at your terminal and paste the output in your comment here. also type `which python` in your terminal and tell me about the output so that i may be able to help you.

      1. I am using OS X 10.8.4. I did not upgrade to Mavericks.

        Do you mean echo $PATH? I get the following:


        and which python gives me:

Leave a Reply to Fuyu Cancel reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s