Install OpenCV 3.0 and Python 3.4+ on OSX


Two weeks ago we kicked off the OpenCV 3.0 install-fest with a tutorial on how to install OpenCV 3.0 and Python 2.7 on OSX.

Today I’m back again with another OSX tutorial — only this time we are going to compile OpenCV 3.0 with Python 3.4+ bindings!

That’s right! With the OpenCV 3.0 gold release, we officially have Python 3 support. It’s been a long time coming — and after being stuck in Python 2.7 land for many years, it’s a great feeling to fire up that Python 3 shell and be part of the future of the Python programming language.

In the rest of this article, I’ll show you how to compile and install OpenCV 3.0 with Python 3.4+ bindings.

UPDATE: The tutorial you are reading now covers how to install OpenCV 3.0 with Python 3.4+ bindings on OSX Yosemite and below. This tutorial still works perfectly if you are using OSX Yosemite or prior, but if you want to install OpenCV on the newer El Capitan and macOS Sierra please use this freshly updated tutorial.

How to Install OpenCV 3.0 and Python 3.4+ on OSX

As I mentioned at the top of this article, I have already covered how to install OpenCV 3.0 with Python 2.7 bindings on OSX in a previous post. As you’ll notice, most of the steps are very similar (and in some cases, identical), so I’ve trimmed down the explanations of each step to reduce redundancy. If you’re looking for more details on each step and command, please see the previous post where I go into a bit more detail.

With that said, let’s go ahead and get started installing OpenCV 3.0 with Python 3.4 bindings on OSX.

Step 1: Install Xcode

Before you can compile OpenCV on your system, you’ll first need to install Xcode, which is a set of developer tools provided by Apple for OSX and iOS development. Installing Xcode is very straightforward and can be accomplished by using the App Store application. Simply search for Xcode, click Get, followed by Install App, and your download will start:

Figure 1: Installing Xcode on OSX.

Figure 1: Installing Xcode on OSX.

The download itself is quite large (> 2gb), and Apple’s content delivery servers aren’t the fastest in the world, so you’ll probably want to go for a walk and get some fresh air while Xcode installs.

Step 2: Setup Homebrew

Homebrew is a package manager for OSX, similar to Ubuntu’s apt-get. We’ll be using Homebrew to help us install and manage some of OpenCV’s prerequisites and dependencies. To install Homebrew, just fire up a terminal and copy and paste the following commands:

Step 3: Install Python 3

OSX doesn’t ship with a copy of Python 3, so we need to install it via Homebrew:

We also need to update our PATH  in ~/.bash_profile  (if the file does not exist, create it) to indicate that Homebrew packages should be used before any system packages:

Let’s reload our ~/.bash_profile  to ensure the changes have taken affect:

And lastly, let’s confirm that Python 3.4 has been installed:

Step 4: Setup our Python 3.4 environment

Now we can focus on setting up our Python 3.4 environment for the OpenCV compilation.

Using virtualenv and virtualenvwrapper is definitely not a requirement to get OpenCV installed on your OSX machine, but I highly recommend using these packages! Being able to create separate Python environments for each of your projects is incredibly useful, so definitely consider using them!

Anyway, let’s install virtualenv  and virtualenvwrapper :

Take note of the pip3  command. I am using pip3  to indicate that the virtualenv  and virtualenvwrapper  packages should be installed for Python 3.4, not Python 2.7 (although these these packages can be used for both Python versions).

We’ll also need to update our ~/.bash_profile  file again by adding the following lines to the bottom of the file:

Followed by reloading our .bash_profile :

Now we’re getting somewhere. Let’s create our cv3  virtual environment that OpenCV will use to compile our Python 3.4 bindings against. This virtual environment will also hold any extra packages we want to store for computer vision and image processing development:

Notice how I am specifying -p python3  to explicitly indicate that we want to create a virtual environment using the Python 3.4 binary.

After the mkvirtualenv  command has executed, we are dropped into our cv3  virtual environment. If you ever want to access this virtual environment again (and as I’ll demonstrate at the bottom of this post), just use the workon  command:

And you’ll be placed back into our cv3  virtual environment.

As far as Python prerequisites go, all we need is NumPy:

Notice how I used pip  instead of pip3  here — since we are in the cv3  virtual environment, the virtualenv  and virtualenvwrapper  scripts are smart enough to know that the pip  associated with the cv3  environment should be used so explicitly using pip3  is not necessary.

Step 5: Install OpenCV prerequisites

In order to compile OpenCV from source, we’ll need a few developer tools:

Along with a few packages to handle reading various image formats from disk:

And another couple packages to optimize various routines inside OpenCV:

Step 6: Compile OpenCV

The first step in compiling OpenCV is to grab the source code from GitHub and checkout the 3.0.0 version:

Update (3 January 2016): You can replace the 3.0.0  version with whatever the current release is (as of right now, it’s 3.1.0 ). Be sure to check for information on the latest release.

We’ll also grab the opencv_contrib package as well which contains extra modules for OpenCV, such as feature detection and local invariant descriptors (SIFT, SURF, etc.), text detection, and more:

Again, make sure that you checkout the same version for opencv_contrib  that you did for opencv  above, otherwise you could run into compilation errors.

Now that the repositories are pulled down, we can create the build  directory:

And use CMake to configure the build itself:

Update (3 January 2016): In order to build OpenCV 3.1.0 , you need to set -D INSTALL_C_EXAMPLES=OFF  (rather than ON ) in the cmake  command. There is a bug in the OpenCV v3.1.0 CMake build script that can cause errors if you leave this switch on. Once you set this switch to off, CMake should run without a problem.

I’ve already explained what each of these options are in my previous post on installing OpenCV 3.0 with Python 2.7 bindings on OSX, so please refer to that post if you want a detailed explanation of each of the arguments.

The gist here is that you want to examine the output of CMake to ensure your Python 3 bindings will be compiled against the Python 3.4 interpreter installed via Homebrew and our cv3  virtual environment:

Figure 2: CMake has picked up the correct Python 3 interpreter, libraries, and site-packages path.

Figure 2: CMake has picked up the correct Python 3 interpreter, libraries, and site-packages path.

Notice how our Python 3 interpreter, Libraries, numpy version, and packages path have been picked up by CMake.

Another good check to make is that python3  is in the list of modules To be built:

Figure 2: A good sanity check to perform prior to kicking off the compile process is to ensure that "python3" is in the list of modules to be compiled.

Figure 3: A good sanity check to perform prior to kicking off the compile process is to ensure that
“python3” is in the list of modules to be compiled.

If you do not see python3  in the list of modules to be built and is in the Unavailable list, then you’ll need to go back to the CMake step and ensure the paths to your PYTHON3_PACKAGES_PATH , PYTHON3_LIBRARY , and PYTHON3_INCLUDE_DIR  are correct. Again, I have provided a detailed explanation of each of these options in my previous post on installing OpenCV 3.0 on OSX, so please refer there for more information.

Assuming CMake has returned without any errors, we can now compile OpenCV:

For a faster compilation, replace the 4 with the number of cores available on your system.

The compile time itself should not take more than 5-10 minutes, so grab a cup of coffee while OpenCV installs, and when you get back (assuming that OpenCV compiled without error, of course), you can install it:

If for some reason you get an error related to having invalid permissions to install OpenCV, just prepend sudo  to the command:

Step 7: Verify the install

At this point, OpenCV 3.0 with Python 3.4+ bindings should be installed on your system. You can verify this by changing directory to where our cv3  virtual environment lives and checking for the  file:

Sure enough, our OpenCV bindings are sitting in the site-packages  directory for our cv3  virtual environment.

Note: You’ll also be able to find the  file (again, which are your OpenCV bindings) in your build/lib  directory as well. The make install  script takes our  file and copies it to our target site-packages  directory.

Finally, let’s verify our install by opening up a shell and importing OpenCV:

Congrats, you now have OpenCV 3.0 with Python 3.4+ bindings installed on your OSX system!


In this tutorial we learned how to compile and intall OpenCV 3.0 with Python 3.4+ bindings by hand using the CMake utility (if you’re looking for a tutorial on installing OpenCV 3.0 with Python 2.7+ bindings, please see my previous post). Granted, compiling by hand is not exactly the most fun experience — but it does give us complete and total control over the install.

Luckily, just a week ago an OpenCV 3.0 install script was created for Homebrew in the science tap. In future posts, I’ll demonstrate how to utilize Homebrew to greatly simplify the compile and install process, making our lives substantially easier.

All that said, I still recommend that you try installing OpenCV 3.0 by hand — Homebrew is nice, but you’ll never get the same full control like when you use CMake.

To be notified when the Homebrew + OpenCV 3.0 install post goes live, please enter your email address in the form below, and I’ll be sure to ping you when the tutorial is published.

, , , ,

141 Responses to Install OpenCV 3.0 and Python 3.4+ on OSX

  1. frank frederic June 29, 2015 at 10:21 pm #

    thank you very much Adrian,
    eagerly waiting for instructions for Linux and RaspPi2 (opencv 3 + python 3)

    • Adrian Rosebrock June 30, 2015 at 6:32 am #

      Thanks Frederic! They should be online within the next 2-3 weeks.

  2. Vlad July 9, 2015 at 7:23 pm #

    I had to add a few options for cmake to make it work. One is to force making the python3 libraries:

    -D BUILD_opencv_python3=ON

    And the other to set the numpy library path correctly:

    -D PYTHON3_NUMPY_INCLUDE_DIRS=/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/numpy/core/include

    So my final command was (I also removed the C examples):

    -D CMAKE_INSTALL_PREFIX=/usr/local \
    -D PYTHON3_PACKAGES_PATH=/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages \
    -D PYTHON3_LIBRARY=/usr/local/Cellar/python3/3.4.3/Frameworks/Python.framework/Versions/3.4/lib/libpython3.4.dylib \
    -D PYTHON3_INCLUDE_DIR=/usr/local/Cellar/python3/3.4.3/Frameworks/Python.framework/Versions/3.4/include/python3.4m \
    -D PYTHON3_NUMPY_INCLUDE_DIRS=/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/numpy/core/include \
    -D OPENCV_EXTRA_MODULES_PATH=~/Projects/opencv_contrib/modules \
    -D BUILD_opencv_python3=ON ..

    • Adrian Rosebrock July 10, 2015 at 6:20 am #

      Hey Vlad, thanks for sharing! However, I will say that the cmake command you shared does not utilize virtual environments which is something a really recommend using. In a future post I’ll also show how to compile and install OpenCV 3 on OSX with Homebrew so that will make life even easier!

      • Nils July 11, 2015 at 4:49 pm #

        +1 for adding -D BUILD_opencv_python3=ON, mine did not find Libraries, numpy, or package path until then.

        My cmake still read:

        Python (for build): /usr/local/bin/python2.7

        I installed anyways and verified that opencv3 and python3 work in my virtualenv. Who knows?

        • Adrian Rosebrock July 12, 2015 at 7:40 am #

          Thanks for the tip Nils — I’ll go ahead and add the -D BUILD_opencv_python3=ON option into the post to ensure CMake always picks it up.

          As for the Python (for build) option, don’t worry about that — it will not update based on which Python version bindings you are compiling. As long as opencv3 is included in the “OpenCV modules” section and your correct version of Python and NumPy have been picked up, that’s all that matters.

  3. Hongtao July 14, 2015 at 9:54 am #

    Hi Adrian,

    When I am compile my opencv, I already receive following error.

    Scanning dependencies of target opencv_python3
    [ 86%] Building CXX object modules/python3/CMakeFiles/opencv_python3.dir/__/src2/cv2.cpp.o
    /Users/hongtaowang/opencv/modules/python/src2/cv2.cpp:6:10: fatal error:
    ‘Python.h’ file not found
    [ 86%] Building CXX object samples/cpp/CMakeFiles/cpp-tutorial-pnp_detection.dir/tutorial_code/calib3d/real_time_pose_estimation/src/CsvReader.cpp.o
    [ 86%] Building CXX object modules/stitching/CMakeFiles/opencv_test_stitching.dir/test/test_blenders.cpp.o
    1 error generated.
    make[2]: *** [modules/python3/CMakeFiles/opencv_python3.dir/__/src2/cv2.cpp.o] Error 1
    make[1]: *** [modules/python3/CMakeFiles/opencv_python3.dir/all] Error 2
    make[1]: *** Waiting for unfinished jobs….

    Do you have any clue for that?

    • Adrian Rosebrock July 14, 2015 at 3:32 pm #

      Hey Hongtao, it looks like the Python development files are not installed on your system. Have you installed Python via brew install python?

      • Hongtao July 15, 2015 at 7:54 am #

        Thanks for your reply. Finally, I found the problem. I do install python through home-brew. The problem is my folder has a different with your tutorial and I didn’t notice that. After correct the folder name, the problem is solved.

        • Adrian Rosebrock July 16, 2015 at 6:30 am #

          Awesome, glad to hear it :-)

        • Hackeron July 21, 2015 at 2:39 pm #

          Ah, the correct command for my system is:

          cmake -D CMAKE_BUILD_TYPE=RELEASE \
          -D CMAKE_INSTALL_PREFIX=/usr/local \
          -D PYTHON3_PACKAGES_PATH=/usr/local/lib/python3.4/site-packages \
          -D PYTHON3_LIBRARY=/usr/local/Cellar/python3/3.4.3_2/Frameworks/Python.framework/Versions/3.4/lib/libpython3.4m.dylib \
          -D PYTHON3_INCLUDE_DIR=/usr/local/Cellar/python3/3.4.3_2/Frameworks/Python.framework/Versions/3.4/include/python3.4m \
          -D BUILD_EXAMPLES=ON \
          -D BUILD_opencv_python3=ON \
          -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules ..

          • Adrian Rosebrock July 21, 2015 at 6:55 pm #

            Thanks for posting the command that worked for you! I’ll be sure to create a FAQ question for this post that helps resolve these issues.

        • anthony July 21, 2015 at 11:06 pm #

          I met the same error, will you tell me more details about how to fix it? I haven’t found the different folder between mine with the tutorial.

      • Akshay July 30, 2015 at 3:42 am #

        Hey Adrian,I’m having the same problem.I did install Python using brew install python..

        /Users/akshay/opencv/modules/python/src2/cv2.cpp:6:10: fatal error: ‘Python.h’
        file not found

        I’ve been trying to get a solution for hours now,and it’s really frustrating.Where have I been going wrong?Can you please help?

        • Adrian Rosebrock July 30, 2015 at 6:37 am #

          It looks like for whatever reason, CMake is not trying your Python header files. Be sure to check the PYTHON3_INCLUDE_DIR switch and ensure that it is pointing to a valid header file. The headers are likely in a sub-directory of /usr/local/Cellar/python3/ like in the CMake command I detailed, but your paths might be slightly different. I would start investigating there.

          • Akshay July 30, 2015 at 11:15 am #

            Thank you soooooo much Adrian!Really appreciate your help!

            Turns out that one of the subdirectories was named 3.4.3_2 instead of 3.4.3.

            Thanks again man!It was a really really good tutorial btw.

          • Adrian Rosebrock July 31, 2015 at 7:09 am #

            Nice! Congrats on fixing that issue! :-)

        • Kyle August 17, 2015 at 11:42 pm #

          Hey, I’m stuck on the same issue…I also had a file named 3.4.3_2 instead of 3.4.3. I corrected this file, but I’m still getting the same error. Did you do anything additional to get the fix? I’ve been banging my head against the wall for hours. Any help is greatly appreciated!!!

          • sarath September 25, 2015 at 2:29 pm #

            Hey, Kyle Did you figure out the solution for the error? I struck at the same issue.

    • mcha April 21, 2016 at 5:27 am #

      1. Thanks Adrain for the tutorial
      2. Thanks Hongtao for posting the solution
      3. I faced the same issue and here is how I solved it:
      I used the command
      python3-config --include to find my header location
      then replaced 3.4 with 3.5 NOTICE, it is in /Library/Frameworks/Py……

      my cmake command became like this:

      -D CMAKE_INSTALL_PREFIX=/usr/local \
      -D PYTHON3_PACKAGES_PATH=~/.virtualenvs/cv3/lib/python3.5/site-packages \
      -D PYTHON3_LIBRARY=/Library/Frameworks/Python.framework/Versions/3.5/lib/libpython3.5m.dylib \
      -D PYTHON3_INCLUDE_DIR=/Library/Frameworks/Python.framework/Versions/3.5/include/python3.5m \
      -D BUILD_opencv_python3=ON \
      -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules ..

      hope it helps

      • shmru June 13, 2016 at 5:00 pm #

        Hey, thanks for the help ! Only your cmake worked for me, and I’m stuck at this problem for like hours!!!

      • Mojo_jojo August 2, 2016 at 11:43 am #

        Thanks, had the same issue. Your solution fixed it!

  4. Diemesleno Souza Carvalho July 20, 2015 at 8:33 pm #

    Wonderful tutorial Adrian,

    Thank you very much.

    I have Anaconda with Python 3.4 in my system, but I followed your tutorial and just changed to my python environment and all worked perfect.


    • Adrian Rosebrock July 21, 2015 at 6:37 am #

      Fantastic, glad to hear it :-)

    • Josh June 28, 2016 at 10:11 pm #

      Thank you for leaving this comment, I was just wondering if doing this exact thing would work!

  5. Freeman Zhang July 29, 2015 at 12:30 pm #

    As python2.7 is installed by default in OS X (10.10), I think it’s better to add one more cmake option:

    (from my experience, it still build python 2.7 module without this option)

    I use the following cmake command with options to build opencv 3.0 and install to VIRTUAL_ENV directory.

    $ PYTHON3_DIR=python3-config --prefix # Get python 3 path
    -D PYTHON3_PACKAGES_PATH=$VIRTUAL_ENV/lib/python3.4/site-packages \
    -D PYTHON3_INCLUDE_DIR=$VIRTUAL_ENV/include/python3.4m \
    -D PYTHON3_LIBRARY=$PYTHON3_DIR/lib/libpython3.4m.dylib \
    -D PYTHON_NUMPY_INCLUDE_DIRS=$VIRTUAL_ENV/lib/python3.4/site-packages/numpy/core/include/numpy
    -D BUILD_opencv_python3=ON \
    -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules ..

    It can avoid conflicts from opencv 2.0 in Homebrew if install opencv 3.0 to another directory instead of /usr/local.

    And add these lines in $VIRTUAL_ENV/bin/postactivate to make sure dynamic libraries of opencv 3.0 can be found:

    if [ “x${DYLD_LIBRARY_PATH}” == “x” ]

    • Adrian Rosebrock July 30, 2015 at 6:39 am #

      Very nice, thanks for the tip! I had not seen the python27 module built in this way, but it’s clear that many others are having different experiences.

    • Nirvana September 13, 2015 at 8:25 pm #

      I kept seeing the following, under the Python 3 section:
      — numpy: NO (Python3 wrappers can not be generated)

      I realized I needed to update “PYTHON_NUMPY_INCLUDE_DIRS” to “PYTHON3_NUMPY_INCLUDE_DIRS”

      • Adrian Rosebrock September 14, 2015 at 6:13 am #

        Thanks for the tip!

  6. Max August 9, 2015 at 6:20 pm #

    Following these instructions exactly and cannot get cmake to build using python 3. consistently reverts to python 2.7 despite all previous steps executing flawlessly. :-/

    • Adrian Rosebrock August 10, 2015 at 6:35 am #

      Hey Max, it definitely sounds like CMake is wanting to build Python 2.7 support. Take a look at Vlad’s comment above, he mentioned a change to the CMake script that worked for him.

  7. zyyw August 26, 2015 at 6:30 pm #

    That’s fantastic!!!

  8. Roger September 1, 2015 at 8:17 pm #

    Hi Adrian,

    thanks for this post. Although I agree that using cmake offers more control and is useful chasing bugs in the brandnew opencv3, for many people starting to play with computer vision the homebrew post will be very attractive as well.
    As for me, I can confirm that instead of using homebrew I installed macports and used the command ‘sudo port install opencv +python34’ to get it working (well, I can import cv2, I don’t know about all the rest, yet).

    • Adrian Rosebrock September 2, 2015 at 5:58 am #

      Hey Roger, I’ll be doing a new blog post on installing OpenCV 3 and all the extras using Homebrew in a few weeks. In general, I don’t recommend using MacPorts. It’s starting to show it’s age Homebrew provides better support.

  9. Eric September 18, 2015 at 5:37 am #

    Hi I followed these directions last week and all worked great. However things got pretty broken after upgrading to Python 3.5.

    I’ve attempted to start from scratch and follow these directions again, but can’t seem to get past the numpy step. After much churning, the error is “Failed building wheel for numpy”. I know there is a lot more output you’d need in order to truly diagnose this issue, but perhaps you have some insight on something you expect to change with regards to Python 3.5?

    • Adrian Rosebrock September 18, 2015 at 7:31 am #

      Hey Eric, I personally haven’t tried upgrading to Python 3.5, mainly because I knew upgrading would break a bunch of things. Basically, any package you installed for Python 3.4 has to be re-compiled and re-installed for Python 3.5. Unfortunately, I do not have any insight into this issue.

      • Steve September 25, 2015 at 4:06 pm #

        I just followed your tutorial using Python 3.5 and had it working after building from scratch. All I did was replace every 3.4.X with either 3.5.0 or 3.5 depending on where the substitution is taking place.

        • Adrian Rosebrock September 26, 2015 at 6:31 am #

          Very nice Steve!

        • Jeff October 6, 2015 at 7:29 pm #

          I had to rename the in site packages from to when compiling with python 3.5.

          also, i had to use pip3 to install numpy despite statements to the contrary above. everything seems in working order now! thanks!!

          • Adrian Rosebrock October 7, 2015 at 6:30 am #

            The is a really strange bug that I run into myself. I’m honestly not sure why that happens. But as you said, renaming to will fix the issue.

  10. Wai Yan October 2, 2015 at 12:03 pm #

    Thank You so much for the step by step tutorial.

    • Adrian Rosebrock October 3, 2015 at 6:13 am #

      No problem, I’m happy it worked for you!

  11. ST October 8, 2015 at 10:45 am #

    Hi Adrian,

    A quick question, say if I install openCV 3 on my mac, will I be able to still use my old openCV (which is already installed with Python 2.7)?


    • Adrian Rosebrock October 9, 2015 at 6:48 am #

      It depends on how you actually install it. If you already have OpenCV 2.4 installed, then run everything in this tutorial up until the make install. From there, all you need to do is keep the opencv/build directory and sym-link the file into your site-packages directory. This is exactly what I do to run multiple OpenCV versions on my own system.

  12. Rick November 5, 2015 at 7:10 pm #

    Adrian, I just got a new iMac running El Capitan, python 3.5 in the office. I followed the instructions without any problem. Thanks a lot. Just one point on the last part of the instructions on checking the location of It is not in ~/.virtualenvs/cv3/lib/python3.5/site-packages/. It is located at /usr/local/lib/python2.7/site-packages/. Other than that, everything is just working smoothly as always.

    • Adrian Rosebrock November 6, 2015 at 6:22 am #

      Hey Rick, thanks for the comment. If the files ended up in the Python 2.7 site-packages directory and Python 3.5 site-packages directory, make sure you double check the output of CMake and ensure you compiled Python 3 bindings (and not Python 2.7 bindings). Other than that, it sounds like you’re good to go!

  13. Neeraj November 19, 2015 at 2:00 pm #

    Hi Adrian,
    I figured out the problem with the compilation issue which I faced yesterday and also posted my error to you, I just figured it out, I was using wrong python3 version and not from the homebrew directory, I think i installed python3 from somewhere else which broke the whole build process earlier, but now i cleaned homebrew and installed, everything worked for me now..
    Thanks for all the help and for publishing such great blogs.Keep up the good work.

    • Adrian Rosebrock November 20, 2015 at 6:29 am #

      Congrats on getting everything working Neeraj!

  14. Jim Selikoff November 28, 2015 at 3:48 pm #

    Adrian, thanks so much for these tutorials! I’m also using Python 3.5, but did not rename and everything worked fine.


    • Adrian Rosebrock November 29, 2015 at 7:10 am #

      I’m glad it worked for you Jim!

  15. Jean Bilheux December 18, 2015 at 3:43 pm #

    This is what I call a good tutorial
    Thanks a lot.

    By the way I tried to install it with python 3.5 just by curiosity… did not work.
    only 3.4+ (as the title says)

    • Adrian Rosebrock December 19, 2015 at 7:48 am #

      You can certainly use this tutorial to install with Python 3.5, but you’ll need to change the CMake command, specifically the PYTHON3_PACKAGES_PATH, PYTHON3_LIBRARY, and PYTHON3_INCLUDE_DIR to point to your 3.5 installation versus the 3.4.

  16. RAD January 3, 2016 at 2:45 am #

    Hi, when I put cmake step I get this error

    What can I Do?

    • Adrian Rosebrock January 3, 2016 at 8:03 am #

      I assume you are compiling OpenCV 3.1.0 instead of OpenCV 3.0.0? If so, just remove the -D INSTALL_C_EXAMPLES=ON from the CMake command and that should fix the issue. There is a bug in the OpenCV 3.1.0 CMake script that is causing this error.

      • RAD January 3, 2016 at 2:19 pm #

        Yes. I’m sorry for this question.
        Thank you so much for you work here.

        • Adrian Rosebrock January 3, 2016 at 2:32 pm #

          No worries, I’m happy I could help out. I have also updated the blog post to mention that the flag should be turned off.

  17. mathivanan January 10, 2016 at 2:04 am #

    some one send me the ball tracking code using python3 and opencv3.

    • Adrian Rosebrock January 10, 2016 at 7:37 am #

      Updating the code to work with Python 3 and OpenCV is pretty trivial:

      1. Update cv2.findContours: (_, cnts, _) = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

      2. And change xrange to range

      And it will work for both Python 3 + OpenCV 3.

      In the future, try to work with the code yourself and ask specific questions for help, rather than posting on an unrelated blog post looking for a “done for you” solution. This is a teaching blog where we all help each other out.

  18. aoy January 10, 2016 at 4:48 am #

    hello can you help me i’m at step 4
    $ pip3 install virtualenv virtualenvwrapper
    and it says
    -bash: /usr/local/bin/pip3: /usr/local/opt/python3/bin/python3.5: bad interpreter: No such file or directory
    what should i do ?

  19. Mike January 17, 2016 at 5:57 pm #

    Hi Adrian, thanks so much for the installation guides. I’ve installed on a variety of Debian/Ubuntu and Raspbian Jessie using your guides, all Python 3.4 and OpenCV 3.0.0. But I probably had the hardest time with Mac OS X, 10.11.2. Brew now installs python3.5.1, and adjusting the locations in the cmake command got it to work, although I had to make a number of edits to the cmake command, which I’ll include below.

    I also had to do brew install opus free type libass to eliminate some link errors.

    Also, while iteratively working out the configuration, it’s necessary to reset the build directory from scratch once in a while, especially when results don’t seem to make sense. I just delete the whole directory and restart.

    After a make install I was able to import cv2 right away.

    Here is a gist for the cmake command that worked for me (note that I put the source under ~/src, and turned off the examples):


    • Adrian Rosebrock January 18, 2016 at 3:23 pm #

      Thanks for sharing Mike!

  20. Philip February 1, 2016 at 5:47 am #

    Hi Adrian

    Thank You so much for an impeccable and extremely useful tutorial. One or too copy-paste glitches didn’t stop me from completing your super set-up! On the OpenCV front, I share your interest in python, search and images with UNMOVIE 2002 which due to security issues is not online ATM, but it will be…

    Cheers, Philip

  21. Anura February 21, 2016 at 11:44 pm #

    Hi Adrian,

    With the help of your blog and respondents, i was able to compile OpenCV 3.1.0 and Python 3.5.1.

    1. I had some problems with Step 4. After combing the net i found the following command and applied it before Step 4
    brew link python3 -v

    Only then i could proceed.

    2. In Step 7 of verify install my .so file is in the form of I used your solution in Step 11 of your Ubuntu page (

    Below is the cmake command with the help of you guys here

    So far the import cv2 works. Going to try the other projects soon.


    • Adrian Rosebrock February 22, 2016 at 4:21 pm #

      Thanks for sharing Anura! Congrats in getting OpenCV installed.

  22. Chen Ruo March 1, 2016 at 2:42 pm #

    Hi Adrian, thank you so much for the tutorial. I managed to install them correctly despite some errors here and there because python is in its 3.5.1 version.

    I am a beginner on both python and openCV, and I just bought my new Macbook. I hope you don’t find my question stupid.

    I used to code on Sublime Text3 using my Windows laptop and I don’t have problem importing. However, I tried to import cv2 after installing successfully, and run the code using Sublime Text 3, it gave me this error “ImportError: No module named cv2”. I realised when I tried import cv2 under the cv3 virtual environment, it is successful. How do I change to this cv3 virtual environment in the code itself so sublime text 3 can actually find the cv2 module? Do I need to change my path to the usr/local/bin to use the Python3?

    • Adrian Rosebrock March 1, 2016 at 3:34 pm #

      Congrats on getting OpenCV installed! Sublime Text doesn’t natively support virtual environments, but I believe there is a plugin for that. Otherwise, you should use Sublime Text as your code editor and then execute the code via command line inside the cv3 virtual environment.

  23. 0mza987 March 13, 2016 at 7:39 am #

    Hi, thank your detailed article very much. I have successfully installed all of the things. But I have one small question : After the installation can I delete the opencv and opencv_contrib files or I have to keep them as long as I need to use opencv functions?

    • Adrian Rosebrock March 13, 2016 at 10:16 am #

      As long as you have executed sudo make install, you can safely delete the opnecv and opencv_contrib directories.

  24. Frank228 March 19, 2016 at 3:34 pm #

    Hi, thank you for your detailed tutorial. I have installed opencv 3.1 successfully on my mac. But I have two more questions: I can only import cv2 module when I run python3 under ~/.virtualenvs/cv3/lib/python3.5/site-packages/ this directory in the terminal. How do I import cv 2 module when python3 is ran under ~ directory. And I use Pycharm IDE for coding, why I can’t import cv2 in pycharm IDE. What shall I do?

    • Adrian Rosebrock March 20, 2016 at 10:42 am #

      Make sure you are in the cv3 virtual environment when running Python:

      As long as you are in the Python virtual environment, the import should work.

      As for using PyCharm with the Python virtual environment, be sure to follow this tutorial.

  25. Jorge March 30, 2016 at 11:25 am #

    Hello, thanks so much for the tutorial, it was easy to follow and worked flawlessly for me! However, after the smooth install of python3, virtualenvs, and openCV3.1.0, I have been running into a few problems, that I think have to do with using virtual environments.

    The first one is that I can’t get matplotlib to work in my virtual environment. I can pip install it and even import matplotlib but when I try to import pyplot, python complains of a problem with the backend. Have tried changing the backend (specifically to Qt4Agg, after brew installing sip and PyQt4) and a load of other solutions I found online but to no avail. Do you have any idea what to do?

    Secondly, I am having some problems using cv2.VideoCapture() with .avi files. Seems to work fine with .mpg and .mp4 but this one .avi file I have (which is encoded using ffmpeg), gives me ret = False whenever I try to read the video (i.e. ret,im = cv2.VideoCapture(‘file.avi’).read())

    If you have any ideas as to how to fix either of these problems, it would be greatly appreciated!!

    • Adrian Rosebrock March 30, 2016 at 12:43 pm #

      Nice, I’m happy to hear the install went well!

      As for your matplotlib issue, this post demonstrates how to resolve it. It is indeed a backend issue and it can be fixed by installing matplotlib from source.

      As for the .avi files, that’s almost certainly an video codec issue. Unfortunately, getting certain video codecs to work are a bit of a dark art. You might want to re-compile and re-install OpenCV with FFMPEG support enabled. But again, this will likely take some trial and error.

  26. Massimo Di Stefano March 31, 2016 at 6:27 am #

    Hi Adrian, I followed your instructions. I’m able to import cv2 in python3 but can’t have sift working. I added a comment on this github issue on the opencv_contrib repository:

    I thought it was related. Have you any clue on how to debug this problem or if I’m missing something?


  27. Wes April 13, 2016 at 2:14 pm #

    Accidentally hit enter. Keyboard struggles.
    1. It looks like the C examples flag works as of posting this.
    2. The latest XCode toolchain 7.3 seems to have broken the build so I am going to try reverting to XCode 7.2.1 and see if that fixes the issue.

    • Adrian Rosebrock April 13, 2016 at 6:48 pm #

      Thanks for sharing Wes!

  28. Sam LEE April 24, 2016 at 12:01 am #

    Hi I have getting trouble to install on my Mac 10.11

    @ Step 6: Compile OpenCV
    I can not cd opencv
    after that everything was not massed.

    Could you please help me to continue from Step 6 ??
    Where is the opencv directory on my mac ?


    • Adrian Rosebrock April 25, 2016 at 2:03 pm #

      The opencv directory should be in the same directory as where you executed the git clone command.

  29. Shreyash Agarwal May 4, 2016 at 11:56 am #

    Hi Adrian, Python is currently at 3.5.1
    Please update your post to include the relevant versions..

    I installed it in Applications/ Folder instead of the root home folder.

    Installed correctly, also had to do rename the .so file at site-packages as it was named something else…


    Then could import it in python3 :)

    Cheers Adrian!
    I’m learning from your blog everyday.

    • Adrian Rosebrock May 4, 2016 at 12:28 pm #

      Thanks for sharing your CMake command Shreyash!

  30. Peter May 20, 2016 at 3:56 am #

    Hey Adrian, I’ve been having troubles with the cmake command, as underneath the Python 3: section, it can’t find the interpreter or the library. I have python 3.5.1, tried the cmake commands listed above, and double checked with my directories. Do you have any idea what I might have missed? Thanks!

    • Adrian Rosebrock May 21, 2016 at 8:15 am #

      This blog post assumes you’re using Python 3.4.3, but no worries! Simply change occurrences of 3.4.x to 3.5.x in your CMake command and it should run without a problem (although you’ll want to double-check that the paths are valid).

  31. JSch May 23, 2016 at 8:36 am #

    Hey Adrian!

    I have found the solution for my previous problem! But now I am not able to use the CV3 in my virtual environment. The thing is even though I copy the to ~/Envs/cv3 folder it doesn’t work! The test you showed at the end of the tutorial runs the phyton shell inside the folder where the cv library is, my question is: How can I put the library inside any other environment? Do I have to repeat the tutorial? If yes what do I have to change?

    Many thanks again for this incredible tutorial Adrian!

    • Adrian Rosebrock May 23, 2016 at 7:23 pm #

      If you would like to use your OpenCV bindings in any other virtual environment, you can simply copy or sym-link the file into the site-packages directory of each virtual environment. As for importing OpenCV, make sure you have executed the workon command before executing python.

  32. Peter May 26, 2016 at 5:14 am #

    Hey Adrian, I made some progress but I keep on running into this error while running make -j4

    “/Users/peter/opencv_contrib/modules/hdf/include/opencv2/hdf/hdf5.hpp:40:10: fatal error:
    ‘hdf5.h’ file not found”

    Do you have any idea how to solve this? Thank you!

    • Adrian Rosebrock May 26, 2016 at 6:18 am #

      For some reason your install of OpenCV wants to compile HDF5 support. I would install it via Homebrew:

      $ brew install hdf5

      You may need to run brew tap homebrew/science before running the above command, but I’m not sure regarding the package structure.

      Then try to re-install OpenCV.

      • Vinay August 10, 2016 at 12:32 am #

        Hi Adrian,
        A very nice tutorial. Thank you.
        I had this issue and I fixed it by installing hdf5 using the following command.

        brew install homebrew/science/hdf5

        • Adrian Rosebrock August 10, 2016 at 9:25 am #

          Thanks for sharing Vinay! It looks like CMake was trying to compile OpenCV with HDF5 support. This normally isn’t enabled by default, but in your case, installing HDF5 would indeed resolve the issue.

  33. Fatima May 27, 2016 at 11:47 am #


    I have a couple of questions.

    1. After we make the virtual environment at the end of step 4, do we stay in that environment for the remainder of the steps?

    2. At the end of step 3, when I ask ‘which python3,’ I get:

    instead of what you get:

    Does this mean I did something wrong when installing python?

    Thank you very much for your help!

    • Adrian Rosebrock May 27, 2016 at 1:25 pm #

      Yes, after you create the virtual environment, you should stay in it for the rest of the steps. As for the /usr/bin/local/python, that is a typo on my part. I will correct that.

  34. Clara May 29, 2016 at 12:20 pm #

    Like many other recent users, I found this tutorial fantastically helpful. However, I also had problems with the -D PYTHON_INCLUDE_DIR command being the wrong link (even though I used brew). I had the same error message as many others:

    /Users/hongtaowang/opencv/modules/python/src2/cv2.cpp:6:10: fatal error:
    ‘Python.h’ file not found

    For me, I had to change my path to /usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/Headers \

    Adrian, could you put a little note (like the one about turning off C examples) in the original post indicating that for later versions people should explore their file structures a bit for the Python.h file, since the path seems to change with different versions of python3? I spent a few hours on the internet googling my error message before I saw all of the responses to this thread.

    • Adrian Rosebrock May 29, 2016 at 1:46 pm #

      Thanks for the suggestion Clara!

  35. Emma May 31, 2016 at 12:44 pm #

    Hello Adrian,

    My python version is 3.5.1
    In Step 7, I enter “$ ls -l”, it show the error:

    ls: No such file or directory

    • Adrian Rosebrock May 31, 2016 at 3:38 pm #

      If your Python version is 3.5.1, then you’ll need to update your CMake command to point to the correct Python version (3.4 was used in this blog post).

  36. Jack Cai May 31, 2016 at 5:34 pm #

    Hi there
    Any chance you can make a tutorial for OpenCV 3 + Python 2.7 or 3.5 on Windows with extra pencv_contrib module. We can easily get OpenCV 3 working with Python 2.7 Visual Studio on Windows but building OpenCV with the extra module seems to be difficult. It would be great if you can help. Thanks.

    • Adrian Rosebrock June 1, 2016 at 3:23 pm #

      In all honesty, it’s been 9+ years since I’ve used a Windows system. I don’t recommend Windows for computer vision development. And truth be told, I don’t want to support Windows on the PyImageSearch blog. I highly recommend Unix-based systems such as OSX and Linux for CV development.

  37. Roberto June 3, 2016 at 5:34 am #

    Hey Adrian, thanks for the great tutorial! It doesn’t seem to work on the system I’m using for work though (although it does for my machine at home).
    In short, the output of cmake says that the python3 module isn’t available. If I go under the ‘Python 3’ section, it looks like cmake has correctly detected the interpreter, the package path and numpy, but it can’t find the library. I’m pretty sure I’m pointing it to the correct libpython3.5.dylib (I also tried libpython3.5m.dylib – not sure what the difference is) but neither seems to work.
    Do you have any ideas?

    • Adrian Rosebrock June 3, 2016 at 2:58 pm #

      This tutorial was designed for Python 3.4, so if you’re using Python 3.5, the paths will undoubtedly change. I don’t have a Python 3.5 installation on a Mac setup (yet), but as soon as I do, I’ll double-check the paths.

      • Roberto June 6, 2016 at 5:40 am #

        You mean change 3.4.3 to 3.5 everywhere? Because that changes, and nothing else. :) pkg-config confirms, and I’m using tab autocomplete to make sure I don’t mistype anything. I don’t think it’s an issue with the paths. I really don’t understand why cmake can’t ‘see’ the library. I wonder if for some reason python 3.5 isn’t supported?

        • Adrian Rosebrock June 7, 2016 at 3:25 pm #

          Python 3.5 is certainly supported, that’s not an issue at all. The issue is most certainly with the paths, so you might need to tinker with them and ensure that they are changed from 3.4 to 3.5. Exactly what the paths change to is highly dependent on your system.

  38. Raj June 7, 2016 at 10:25 am #

    Hi Adrian, import cv2 works well with Pycharm though it fails in Terminal, how do I fix it?

    • Adrian Rosebrock June 7, 2016 at 3:12 pm #

      It sounds like you may not be accessing the cv3 virtual environment before executing your Python script:

  39. Rauf July 14, 2016 at 10:14 am #

    Hello! Thank you very much for a very helpful tutorial! I’m new in OpenCV and I have a question, maybe you know an answer : how to run the examples in the “text” extra module in C ++?

  40. Hilman July 27, 2016 at 5:14 am #

    Adrian, I have a question. Successfully installed the OpenCV for Python 3 (in a virtual env of cv3) as your tutorial, is it possible for me to make a Python 2 based virtual env, install dependencies needed by OpenCV, and just symlink the file to it?

    Thanks in advance.

    • Adrian Rosebrock July 27, 2016 at 1:56 pm #

      You are correct — you would need to create a Python 2 virtual environment and install NumPy. However, your file is only compatible with your Python 3 install. You would need to re-compile OpenCV 3 with Python 2 support, then sym-link the file from your build directory.

  41. Steve August 1, 2016 at 6:04 pm #

    Thanks worked like a charm! In Step 7 since you installed virtualenvwrapper you can type (all one word):

    to change to the virtualenv site-packages directory. Then ls should show the file.

    • Adrian Rosebrock August 2, 2016 at 3:05 pm #

      Nice, I didn’t know about the cdsitepackages command — thanks for sharing that tip Steve!

  42. Mona Jalal August 3, 2016 at 11:28 pm #

    Hi Adrian,
    Can you please have a look at this issue? For me cmake, make -j4 and sudo make install works but still can’t import cv2.
    Any thought on what is missing or what I should do to fix this would be really helpful.
    Mona Jalal

    • Adrian Rosebrock August 4, 2016 at 10:09 am #

      Hi Mona — unfortunately, OpenCV can be a real pain to install. However, because of this, I can only support the instructions I provide here on the PyImageSearch blog. I hope you understand!

  43. Navin August 6, 2016 at 9:42 am #

    As a victim of the #include file not found error. Make sure to run python-config –prefix to check youR python3 installation path. At the time of me installing it it was 3.5.2_1 and had to change the cmake command to this and it WORKED, YAY !.

    • Adrian Rosebrock August 7, 2016 at 8:14 am #

      Awesome, thanks for sharing Navin! :-)

  44. Ash August 7, 2016 at 5:44 pm #

    Hey Adrian!
    I was able to follow along this tutorial until step 7. At that point when I entered “ls -l” I got “ls: No such file or directory” from the terminal. After looking in the “~/.virtualenvs/cv3/lib/python3.4/site-packages/” directory I found that I have instead of As it stands, I can’t seem to import cv2. I was wondering if there was anything I could do to work around this problem?

    • Adrian Rosebrock August 8, 2016 at 6:40 pm #

      Hey Ash — it looks like you have your OpenCV bindings compiled correctly. All you need to do is rename to That will fix the issue.

  45. Ricardo August 18, 2016 at 9:04 am #

    Hey Adrian! nice post, but i was wondering, is there a way to remove/delete all the files created on the process?, in case everything goes wrong?


    • Adrian Rosebrock August 18, 2016 at 9:23 am #

      During the compile? Simply delete the build directory if you would like to restart the process.

  46. Gregory Pierce August 21, 2016 at 11:19 am #

    Note. These instructions will not build OpenCV on OSX 10.12, you will encounter issues with QTKit. You will have to disable videoio by adding this to your cmake “-D BUILD_opencv_videoio=OFF”

    • Adrian Rosebrock August 22, 2016 at 1:31 pm #

      Thanks for sharing this tidbit Gregory. Does OSX 10.12 come pre-installed with QTKit? Or did you install it on your machine?

  47. Scott September 22, 2016 at 1:52 am #

    First, thanks for the very detailed instructions. This is very helpful.

    Adrian, on Step 6:
    $ cd ~
    $ git clone

    At this point, are you still within the virtual environment?

    In terminal input lines are predicated with (cv3) and I just want to make sure I’m in the right place before continuing.

    • Adrian Rosebrock September 23, 2016 at 6:59 am #

      During the git clone steps it’s not strictly necessary to be in the virtual environment; however, I would suggest that you do stay in the virtual environment because you’ll soon be running CMake which does assume you are in the Python virtual environment.

      • Scott McAndrew September 23, 2016 at 2:29 pm #

        Appreciate that timely feedback. Thanks Adrian!

        • Adrian Rosebrock September 27, 2016 at 8:57 am #

          No worries Scott, happy I could help :-)

  48. Arthur Gilbert October 4, 2016 at 11:58 pm #

    Hi, Adrian. I’m at the stage compiling openCV (make -j4). Suddenly there is this error when I’m at 28%:
    ~/opencv/modules/videoio/src/ fatal error: ‘QTKit/QTKit.h’ file not found

    Do you know what it is and what should I do ?

    • Adrian Rosebrock October 6, 2016 at 6:57 am #

      This is a problem with the current release of OpenCV on El Capitan and macOS Sierra systems. There has been a fixed released but it hasn’t been committed to a version number yet. Instead of doing git checkout 3.0.0 simply just skip that step and work with the development version — this should allow you to avoid the error.

      • Arthur Gilbert October 6, 2016 at 9:51 pm #

        What if I’ve done the git checkout ? Do I need to uninstall everything ?

        • Adrian Rosebrock October 7, 2016 at 7:22 am #

          After you’ve done the git checkout simply delete your “build” directory, recreate it, and re-run CMake and make.

  49. Rusu Valentin October 12, 2016 at 7:43 am #

    Hi,Adrian! i have a problem at the last stage
    fatal error: ‘QTKit/QTKit.h’ file not found
    Can you help me resolv it? i have tryed everything…i have deleted build directory, added this to my cmake “-D BUILD_opencv_videoio=OFF”,but i still have the same error…I have macOS Sierra 10.12.
    What should i do to fix it?

    • Adrian Rosebrock October 13, 2016 at 9:16 am #

      I’m in the process of working on a new tutorial to handle installing OpenCV on El Capitan and macOS Sierra. It has become a real pain in the ass for the newer versions of the Mac operating system (hence why I need to create a new tutorial). In the meantime, I would suggest not using Sierra. If you decide to use Sierra you can compile from source by checking out the latest version of opencv and opencv_contrib from their respective GitHubs.

      You should also read this comment thread on a separate PyImageSearch blog to help you out.

      • Rusu Valentin October 13, 2016 at 12:21 pm #

        Thank you for help! i don’t have anymore that error,but now i have fatal error:
        ‘Python.h’ file not found…

        • Adrian Rosebrock October 15, 2016 at 9:59 am #

          It sounds like you don’t have your Python development headers installed or CMake isn’t configured to point to them. Which version of Python are you using? Python 3.4.x? Python 3.5.x? Keep in mind that you’ll need to update your paths in CMake to point to your specific version of Python.

          • Rusu Valentin October 18, 2016 at 8:21 am #

            I’m using Python 3.5.2.I don’t realy know how to update paths in CMake…

          • Adrian Rosebrock October 20, 2016 at 8:56 am #

            Go back to the CMake command and replace occurrences of Python 3.4 with the correct paths for 3.5 (use tab completion to help you). Otherwise, I would suggest waiting for my updated blog post on installing OpenCV + Python 3.5 bindings on OSX.

      • Hao November 15, 2016 at 3:40 am #

        Hi Adrian,
        thx for your tutorial as it’s very helpful. I appreciate that you will write a new for Mac Sierra.
        To follow this very tutorial, i strongly recommend everyone to use Xcode 7.x combined with macOS Yosemite or El Capitan, as .

        Thanks again for your work!

        • Adrian Rosebrock November 16, 2016 at 1:46 pm #

          Hi Hao — the macOS Sierra tutorials will be published on November 28th and December 5th.

          • Mac Wang November 23, 2016 at 10:49 am #

            Really looking forward to it. I’ve spent couple nights on this. (opencv3 + python3.5 + MacOS Sierra)

          • Adrian Rosebrock November 23, 2016 at 3:18 pm #

            I’m not going to lie — they weren’t easy tutorials to put together. They are well worth it though :-)

  50. Jeepz October 21, 2016 at 8:46 pm #

    I’m attempting to install OpenCV 3.1.0 with Python 3.5.2

    I get these two errors.

    which python 3

    ^ should the 3 after python not be there ?

    Also, I’ve having trouble installing numpy. Does this version of Python come with pip ? I’ve looked around but can’t seem to be able to install it with the

    • Adrian Rosebrock October 23, 2016 at 10:17 am #

      If you are executing the command:

      $ which python3

      Then yes, the “3” should be there. Once you create your virtual environment using mkvirtualenv you can leave off the “3”.

      As for installing NumPy, create your virtual environment and then run the command:

      $ pip install numpy

  51. Willis January 1, 2017 at 4:36 pm #

    When using the cmake, i’ve tried three times. There always be on error called fatal error ‘QTKit/QTKit.h’ not find. I have no idea what happen. so could you please help me figure it out?

    • Adrian Rosebrock January 4, 2017 at 11:04 am #

      It sounds like you’re using macOS instead of the older OSX. Please see this blog post to the solution to the QTKit problem.

Leave a Reply