OpenCV in a Virtualenv

In an earlier post I outlined how to get set up for computer vision in Python. There, I skipped over one important component: installing OpenCV.

Partly, I've separated this to its own post because it's large enough to be a topic of its own. But mainly it's because you can actually get quite far without ever needing OpenCV. However, as I found out this weekend, if you want to do any work with video, you will pretty much be forced to use OpenCV.1 OpenCV makes it really easy to both extract individual frames of the video and draw visualizations on top of them.

Installing OpenCV is highly system-dependent, so here I will focus on OS X (as usual). The official documentation covers Windows and Linux well enough, anyway.

Two caveats here:

  1. You must install NumPy globally in order to install OpenCV with Homebrew.
  2. It's 2014 and OpenCV still doesn't work with Python 3!!2

Homebrew may warn you that you need NumPy installed first. Unfortunately you will be forced to install NumPy globally. Most scientists who hack together vision systems probably don't give a hoot about this, but I like to keep my system clean with virtualenvs. It's not a big deal though, because admittedly it would be useful to have NumPy available on the global level. Plus, I can always uninstall NumPy later; we just need it for the duration of the build process.

I tried installing while under a virtualenv, but for some reason the build did not create the necessary shared object (.so) files in my site-packages. So, we'll install both NumPy and OpenCV globally, and then copy cv to wherever we need it. Unfortunately we'll have to do this every time we create a new virtualenv for OpenCV. :-/ 3

So, make sure you are not inside a virtualenv, then issue the following commands:

Let's see if it works:

If that didn't elicit any errors, then you're golden!


  1. Your other two options are using ffmpeg by manually calling out to the command line, or to do the video parsing yourself (which is actually pretty easy if it's an .avi file). There used to be python bindings for ffmpeg, but those are now defunct.
  2. But it's coming in version 3.0—which at the time of writing is already a month and a half late.
  3. Someone on StackOverflow told me that they had no problems quarantining OpenCV to a virtualenv. So maybe you should try it yourself and see if it works?
Tagged , , , , , . Bookmark the permalink.

6 Responses to OpenCV in a Virtualenv

  1. Adrian says:

    This is a fantastic post, thanks for putting this together. It definitely seems like it's going to be awhile until OpenCV supports Python 3, but at the same time, the scientific community seems quite happy with 2.7, and I don't think they feel the need to change just yet. It will definitely be a slow process.

  2. Old Fart says:

    Can't pip install opencv? How quaint.

  3. Liza Daly says:

    Thanks, this helped me a ton. I was installing it in a virtualenv and despite no errors, the packages were never being truly available.

  4. azh says:

    /usr/local/lib/python2.7/site-packages : this folder for me is empty. What should I do?

  5. ntraft says:

    @azh: The files are cv.py and cv2.so. See if you can find them elsewhere on your filesystem.

  6. Super helpful, thanks for the article!

Leave a Reply

Your email address will not be published. Required fields are marked *