Setting up OpenCV3 with Python on MacOS

OpenCV is an open source library with many useful functions for manipulating images and arrays. For my thesis, I had to research of Background Subtraction techniques, a category within Computer vision which forms the basis for detecting objects in video streams. In order to complete my research, I had to test the performance and complexity of various Background Subtraction techniques. My initial attempts involved using MATLAB to complete my research, but after learning about this useful API, I decided to test it out but it would have been way too time consuming to set OpenCV with more traditional programming languages like C so I decided to test it out with python…


Setting up OpenCV with Python has been alot more straight forward using Homebrew. After install homebrew, you need to install python and opencv3 respectively via Terminal.

brew install python
brew install opencv3

If you want to have the IDLE IDE available in your applications for quick launching you just need to enter

brew linkapps

After installing python you can check which python installation you are using by entering

which python

If the response is


then you are using the version installed by Homebrew.

If the response is


then you are using the native installation of python which isn’t really recommended to use.

Note: if you want to use the native installation of python without uninstalling the brew version you just need to enter the following into terminal:

brew unlink python

Configuring Python with Virtual Env

Pip, which is like python’s native package manager will be installed alongside python. A good way of setting up multiple environments in python is to use virtualenv and virtualenvwrapper. To install:

pip install virtualenv virtualenvwrapper

After installation, the following needs to be added to the bash profile,( a text file which configures the shell whenever it starts) by entering

nano ~/.bash_profile

and pasting the following: 

export WORKON_HOME=~/Envs
source /usr/local/bin/

A simple re-opening of terminal will implement these settings or

source ~/.bash_profile

Now that the virtualenv package is installed, all environments will be installed in the ~/Envs directory. To work on a new package simply enter

mkvirtualenv [ENV NAME]

and to work on [ENV NAME]

workon [ENV NAME]

Deactivate the environment with


and entering the environment directory with


Linking OpenCV3 To Python

The final step is to link OpenCV to Python so Python knows where to call the functions from. Still in terminal, enter:

echo /usr/local/opt/opencv3/lib/python2.7/site-packages >> /usr/local/lib/python2.7/site-packages/opencv3.pth

mkdir -p /Users/[USERNAME]/Library/Python/2.7/lib/python/site-packages

echo 'import site; site.addsitedir("/usr/local/lib/python2.7/site-packages")' >> /Users/[USERNAME]/Library/Python/2.7/lib/python/site-packages/homebrew.pth


To verifiy that opencv is working with python

start python and enter

import cv2

Note: to start an IDLE session with the virtual environment settings, activate the virtual environment and run the following python script


Leave a 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