macOS: Install OpenCV 3 and Python 3.5


Last week I covered how to install OpenCV 3 with Python 2.7 bindings on macOS Sierra and above.

In today’s tutorial we’ll learn how to install OpenCV 3 with Python 3.5 bindings on macOS.

I decided to break these install tutorials into two separate guides to keep them well organized and easy to follow.

To learn how to install OpenCV 3 with Python 3.5 bindings on your macOS system, just keep reading.

macOS: Install OpenCV 3 and Python 3.5

As I mentioned in the introduction to this post, I spent last week covering how to install OpenCV 3 with Python 2.7 bindings on macOS.

Many of the steps in last week’s tutorial and today’s tutorial are very similar (and in some cases identical) so I’ve tried to trim down some of the explanations for each step to reduce redundancy. If you find any step confusing or troublesome I would suggest referring to the OpenCV 3 + Python 2.7 tutorial where I have provided more insight.

The exception to this is “Step #7: Configure OpenCV 3 and Python 3.5 via CMake on macOS” where I provide an extremely thorough walkthrough on how to configure your OpenCV build. You should pay extra special attention to this step to ensure your OpenCV build has been configured correctly.

With all that said, let’s go ahead and install OpenCV 3 with Python 3.5 bindings on macOS.

Step #1: Install Xcode

Before we can compile OpenCV on our system, we first need to install Xcode, Apple’s set of software development tools for the Mac Operating System.

The easiest method to download Xcode is to open up the App Store application on your desktop, search for “Xcode” in the search bar, and then click the “Get” button:

Figure 1: Downloading and installing Xcode on macOS.

Figure 1: Downloading and installing Xcode on macOS.

After installing Xcode you’ll want to open up a terminal and ensure you have accepted the developer license:

We also need to install the Apple Command Line Tools. These tools include programs and libraries such as GCC, make, clang, etc. You can use the following command to install the Apple Command Line Tools:

When executing the above command a confirmation window will pop up asking for you to confirm the install:

Figure 2: Installing the Apple Command Line Tools on macOS.

Figure 2: Installing the Apple Command Line Tools on macOS.

Click the “Install” button to continue. The actual installation process should take less than 5 minutes to complete.

Step #2: Install Homebrew

The next step is to install Homebrew, a package manager for macOS. You can think of Homebrew as the macOS equivalent of Ubuntu/Debian-based apt-get.

Installing Homebrew itself is super easy, just copy and paste the entire command below:

Once Homebrew is installed you should make sure the package definitions are up to date by running:

We now need to update our ~/.bash_profile  file (or create it if it doesn’t exist already). Open up the file using your favorite text editor (I’m using nano  in this case):

And then add the following lines to the file:

This export  command simply updates the PATH  variable to look for binaries/libraries along the Homebrew path before the system path is consulted.

I have included a screenshot of what my ~/.bash_profile  looks like as reference below:

Figure 3: Updating my .bash_profile file to include Homebrew.

Figure 3: Updating my .bash_profile file to include Homebrew.

After updating your .bash_profile  file, save and exitor the editor followed by using source  to ensure the changes to the .bash_profile  are manually reloaded:

This command only needs to be executed once. Anytime you open up a new terminal your .bash_profile  will automatically be source ‘d for you.

Step #3: Setup Homebrew for Python 3.5 and macOS

It is considered bad form to develop against the system Python as your main interpreter. The system version of Python should serve only one purpose — support system routines and operations. There is also the fact that macOS does not ship with Python 3 out of the box.

Instead, you should install your own version of Python that is independent from the system install. Using Homebrew, we can install Python 3 using the following command:

Note: Make sure you don’t forget the “3” in “python3”. The above command will install Python 3.5 on your system. However, if you leave off the “3” you’ll end up installing Python 2.7.

After the Python 3 install completes we need to create some symbolic links:

As a sanity check, it’s important to confirm that you are using the Homebrew version of Python 3 rather than the system version of Python 3. To accomplish this, simply use the which  command:

Important: Inspect this output closely. If you see /usr/local/bin/python3  then you are correctly using the Homebrew version of Python. However, if the output is /usr/bin/python3  then you are incorrectly using the system version of Python.

If you find yourself using the system version of Python instead of the Homebrew version you should:

  1. Ensure Homebrew installed without error.
  2. Check that brew install python3  finished successfully.
  3. You have properly updated your ~/.bash_profile  and reloaded the changes using source . This basically boils down to making sure your ~/.bash_profile  looks like mine above in Figure 3.

Step #4: Install Python virtual environments and NumPy

We’ve made good progress so far. We’ve installed a non-system version of Python 3 via Homebrew. However, let’s not stop there. Let’s install both virtualenv and virtualenvwrapper so we can create separate, independent Python environments for each project we are working on — this is considered a best practice when developing software in the Python programming language.

I’ve already discussed Python virtual environments ad nauseam in previous blog posts, so if you’re curious about how they work and why we use them, please refer to the first half of this blog post. I also highly recommend reading through this excellent tutorial on the blog that takes a deeper dive into Python virtual environments.

Install virtualenv and virtualenvwrapper

Installing both virtualenv  and virtualenvwrapper  is a snap using pip :

After these packages have been installed we need to update our ~/.bash_profile  again:

Once opened, append the following lines to the file:

After updating, your ~/.bash_profile  should look similar to mine:

Figure 4: Updating your .bash_profile file to include virtualenv/virtualenvwrapper.

Figure 4: Updating your .bash_profile file to include virtualenv/virtualenvwrapper.

After updating your .bash_profile , save it, exit, and then once again source  it:

I’ll reiterate that this command only needs to be executed once. Each time you open up a new terminal window this file will automatically be source ‘d for you.

Create your Python 3 virtual environment

We can now use the mkvirtualenv  command to create a Python 3 virtual environment named cv :

The -p python3  switch ensures that a Python 3 virtual environment is created instead of a Python 2.7 one.

Again, the above command will create a Python environment named cv  that is independent from all other Python environments on your system. This environment will have it’s own site-packages  directory, etc., allowing you to avoid any type of library versioning issues across projects.

The mkvirtualenv  command only needs to be executed once. To access the cv  Python virtual environment after it has been created, just use the workon  command:

To validate that you are in the cv  virtual environment, just examine your command line. If you see the text (cv)  preceding the prompt, then you are are in the cv  virtual environment:

Figure 6: Make sure you see the "(cv)" text on your prompt, indicating that you are in the cv virtual environment.

Figure 5: Make sure you see the “(cv)” text on your prompt, indicating that you are in the cv virtual environment.

Otherwise, if you do not see the cv  text, then you are not in the cv  virtual environment:

Figure 7: If you do not see the "(cv)" text on your prompt, then you are not in the cv virtual environment and you need to run the "workon" command to resolve this issue before continuing.

Figure 6: If you do not see the “(cv)” text on your prompt, then you are not in the cv virtual environment and you need to run the “workon” command to resolve this issue before continuing.

If you find yourself in this situation all you need to do is utilize the workon  command mentioned above.

Install NumPy

The only Python-based prerequisite that OpenCV needs is NumPy, a scientific computing package.

To install NumPy into our cv  virtual environment, ensure you are in the cv  environment (otherwise NumPy will be installed into the system version of Python) and then utilize pip  to handle the actual installation:

Step #5: Install OpenCV prerequisites using Homebrew

OpenCV requires a few prerequisites to be installed before we compile it. These packages are related to either (1) tools used to build and compile, (2) libraries used for image I/O operations (i.e., loading various image file formats from disk such as JPEG, PNG, TIFF, etc.) or (3) optimization libraries.

To install these prerequisites for OpenCV on macOS execute the following commands:

Step #6: Download the OpenCV 3 source from GitHub

As I detailed in last week’s tutorial, OpenCV 3 on macOS needs to be compiled via the latest commit to GitHub instead of an actual tagged release (i.e., 3.0, 3.1, etc.). This is because the current tagged releases of OpenCV do not provide fixes for the QTKit vs. AVFoundation errors (please see last week’s blog post for a thorough discussion on this).

First, we need to download the OpenCV GitHub repo:

Followed by the opencv_contrib repo:

Step #7: Configure OpenCV and Python 3.5 via CMake on macOS

This section of the tutorial is the most challenging and the one that you’ll want to pay the most attention to.

First, I’ll demonstrate how to setup your build by creating the a build  directory.

I then provide a CMake template that you can use to start the process of compiling OpenCV 3 with Python 3.5 bindings on macOS. This template requires you to fill in two values:

  1. The path to your libpython3.5.dylib  file.
  2. The path to your Python.h  headers for Python 3.5.

I will help you find and determine the correct values for these paths.

Finally, I provide a fully completed CMake command as an example. Please note that is command is specific to my machine. Your CMake command may be slightly different due to the paths specified. Please read the rest of this section for details.

Setting up the build

In order to compile OpenCV with Python 3.5 bindings for macOS we first need to set up the build. This simply amounts to changing directories and creating a build  directory:

OpenCV 3 + Python 3.5 CMake template for macOS

The next part, where we configure our actual build, gets a little tricky. In order to make this process easier I have constructed the following OpenCV 3 + Python 3.5 CMake template:

Looking at this template I want to point out a few things to you:

  1. BUILD_opencv_python2=OFF : This switch indicates that we do not want to build Python 2.7 bindings. This needs to be explicity stated in the CMake command. Failure to do this can cause problems when we actually run CMake.
  2. BUILD_opencv_python3=ON : We would like for OpenCV 3 + Python 3.5 bindings to be built. This instruction indicates to CMake that the Python 3.5 binding should be built rather than Python 2.7.
  3. PYTHON3_LIBRARY=YYY : This is the first value that you need to fill in yourself. You need to replace YYY  with the path to your libpython3.5.dylib  file. I will hep you find the path to this value in the next section.
  4. PYTHON3_INCLUDE_DIR=ZZZ : This is the second value that you need to fill in. You will need to replace ZZZ  with the path to your Python.h  headers. Again, I will help you determine this path.

Determining your Python 3.5 library and include directory

We will start by configuring your PYTHON3_LIBRARY  value. This switch should point to your libpython3.5.dylib  file. This file is located within many nested subdirectories of /usr/local/Cellar/python . To find the exact path to the libpython3.5.dylib  file, just use the ls  command with a wildcard (auto-tab complete also works as well):

Take note of the output of this command — this is the full path to your libpython3.5.dylib  file and will replace YYY  in the CMake template above.

Let’s move along to determining the PYTHON3_INCLUDE_DIR  variable. This path should point to the Python.h  header files for Python 3.5 used to generate the actual OpenCV 3 + Python 3.5 bindings.

Again, we’ll use the same ls  and wildcard trick here to determine the proper path:

The output of the ls -d  command is our full path to the Python.h  headers. This value will replace ZZZ  in the CMake template.

Filling in the CMake template

Now that we’ve determined the PYTHON3_LIBRARY  and PYTHON3_INCLUDE_DIR  values we need to update the CMake command to reflect these paths.

On my machine, the full CMake command to configure my OpenCV 3 + Python 3.5 build looks like:

However, please do not copy and paste my exact CMake command — make sure you have used the instructions above to properly determine your PYTHON3_LIBRARY  and PYTHON3_INCLUDE_DIR  values.

Once you’ve filled in these values, execute your cmake  command and your OpenCV 3 + Python 3.5 build will be configured.

As an example, take a look at the Python 3  section output from my configuration:

Figure 7: Ensuring that Python 3.5 will be used when compiling OpenCV 3 for macOS.

Figure 7: Ensuring that Python 3.5 will be used when compiling OpenCV 3 for macOS.

In particular, you’ll want to make sure that:

  1. The Interpreter  points to the Python binary in your cv  virtual environment.
  2. Libraries  points to your libpython3.5.dylib  file.
  3. The numpy  version being utilized is the one you installed in your cv  virtual environment.

Step #8: Compile and install OpenCV 3 on macOS

After investigating your cmake  command and ensuring it exited without error (and that the Python 3  section was properly configured), you can now compile OpenCV:

In this case, I am supplying -j4  to compile OpenCV using all four cores on my machine. You can tune this value based on the number of processors/cores you have.

OpenCV can take awhile to compile, anywhere from 30-90 minutes, depending on your system specs. I would consider going for a nice long walk while it compiles.

A successful compile will end with a 100% completion:

Figure 9: Successfully compiling OpenCV 3 from source with Python 2.7 bindings on macOS.

Figure 8: Successfully compiling OpenCV 3 from source with Python 3.5 bindings on macOS.

Assuming that OpenCV compiled without error, you can now install it on your macOS system:

Step #9: Rename and sym-link your OpenCV 3 + Python 3.5 bindings

After running sudo make install  your OpenCV 3 + Python 3.5 bindings should be located in /usr/local/lib/python3.5/site-packages . You can verify this by using the ls  command:

I’ve been perplexed by this behavior ever since OpenCV 3 was released, but for some reason, when compiling OpenCV with Python 3 support enabled the output  bindings are named differently. The actual filename will vary a bit depending on your system architecture, but it should look something like .

Again, I don’t know exactly why this happens, but it’s an easy fix. All we need to do is rename the file to :

After renaming  to  we then need to sym-link our OpenCV bindings into the cv  virtual environment for Python 3.5:

Step #10: Verify your OpenCV 3 install on macOS

To verify that your OpenCV 3 + Python 3.5 installation on macOS is working you should:

  1. Open up a new terminal.
  2. Execute the workon  command to access the cv  Python virtual environment.
  3. Attempt to import the Python + OpenCV bindings.

Here are the exact steps you can use to test the install:

Note: Take note of the -dev  in the cv2.__version__ . This indicates that we are using the development version of OpenCV and not a tagged release. Once OpenCV 3.2 is released these instructions can be updated to simply download a .zip of the tagged version rather than having to clone down the entire repositories. 

I’ve also included a screenshot below that utilizes these same steps. As you can see, I can access my OpenCV 3 bindings from Python 3.5 shell:

Figure 9: Ensuring that I have successfully installed my OpenCV 3 + Python 3.5 bindings on macOS.

Figure 9: Ensuring that I have successfully installed my OpenCV 3 + Python 3.5 bindings on macOS.

Congratulations, you have installed OpenCV with Python 3.5 bindings on your macOS system!

So, what’s next?

Congrats! You now have a brand new, fresh install of OpenCV on your macOS system — and I’m sure you’re just itching to leverage your install to build some awesome computer vision apps…

…but I’m also willing to bet that you’re just getting started learning computer vision and OpenCV, and probably feeling a bit confused and overwhelmed on exactly where to start.

Personally, I’m a big fan of learning by example, so a good first step would be to have some fun and read this blog post on detecting cats in images/videos. This tutorial is meant to be very hands-on and demonstrate how you can (quickly) build a Python + OpenCV application to detect the presence of cats in images.

And if you’re really interested in leveling-up your computer vision skills, you should definitely check out my book, Practical Python and OpenCV + Case Studies. My book not only covers the basics of computer vision and image processing, but also teaches you how to solve real-world computer vision problems including face detection in images and video streamsobject tracking in video, and handwriting recognition.


So, let’s put that fresh install of OpenCV 3 on your macOS system to good use — just click here to learn more about the real-world projects you can solve using Practical Python and OpenCV.


In this tutorial you learned how to compile and install OpenCV 3 with Python 3.5 bindings on macOS Sierra.

To accomplish this, we configured and compiled OpenCV 3 by hand using the CMake utility. While this isn’t exactly the most “fun” experience, it does give us complete and total control over the install.

If you’re looking for an easier way to get OpenCV installed on your Mac system be sure to stay tuned for next week’s blog post where I demonstrate how to install OpenCV on macOS using nothing but Homebrew.

To be notified when this blog 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.

, , , ,

94 Responses to macOS: Install OpenCV 3 and Python 3.5

  1. Murtaza Dalal December 5, 2016 at 1:01 pm #

    Hi adrian,
    I have a question regarding using c++ opencv. Are we supposed to have a separate virtual environment for that? What is the proper technique for installing and using c++ opencv on mac os Sierra? -the cake commands etc, as well as how to compile the c++ code for opencv. Do you think you could do a tutorial on that? Currently I’m just using c++ opencv on a virtual environment created from the python 2 tutorial and it works but I am not sure that I’m doing things correctly by using a python virtual environment for c++.

    • Adrian Rosebrock December 5, 2016 at 1:22 pm #

      C++ doesn’t have a concept of a virtual environment in the same context as Python. This guide demonstrates how to use Python with OpenCV bindings, but natively OpenCV is a C++ library. You simply compile it, install it, and then use gcc/g++ to link against it when compiling your C++ code.

  2. Taka December 5, 2016 at 2:03 pm #

    Thank you, it works great!

    • Adrian Rosebrock December 7, 2016 at 9:54 am #

      Glad to hear it Taka! Congrats on getting OpenCV installed on your system.

  3. Malcolm Swan December 7, 2016 at 2:53 pm #

    Hi Adrian,
    From Step #5 on are we supposed to be in the virtual environment (cv) as in Step #4? I’ve just completed Step #6 and realised that I’m still in the virtual environment. If we’re supposed to exit the virtual environment before moving onto Step #5 will deleting and recreating the virtual environment get me back on track?


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

      Once you have entered the virtual environment you don’t need to leave it — you can stay inside it.

      If you ever need to access the virtual environment again, just use the workon command (don’t delete it and recreate it):

      $ workon your_virtual_env

  4. Tom Lorent Bourdo December 8, 2016 at 4:01 am #

    Hello, i have a problem during the step 7, my librairy isn’t having the right path as I stupidly used YYY and now can’t change it, do you have any idea of how could I get the path to my dylib file ?

    • Adrian Rosebrock December 10, 2016 at 7:26 am #

      Hey Tom — just delete your build directory, re-create it, and then re-run CMake with the correct path to your .dylib file.

  5. Jacob December 8, 2016 at 2:08 pm #

    Thanks for this great tutorial! Everything worked well.

    What are the next steps to get OpenCV working in Pycharm 2016.3?

    • Adrian Rosebrock December 10, 2016 at 7:25 am #

      Once you have OpenCV installed, please refer to this tutorial on configuring PyCharm.

  6. Nick Machairas December 8, 2016 at 3:14 pm #

    Hello Adrian, thank you for your thorough instructions. I followed your guide with the minor difference that for this specific application I did not want to work in a virtual environment. I made all necessary changes to PYTHON3_LIBRARY, PYTHON3_INCLUDE_DIR, PYTHON3_EXECUTABLE to point to the correct directories. What’s weird is that the was inside the OpenCV git repo instead of my local python site-packages folder. I renamed the .so file and placed it to the correct folder. This allows me to verify that opeCV is installed as in Step#10 above. I haven’t tested functionality yet, I’m about to start with one of your blog posts. I hope it will work.

    Thanks again!

    • Adrian Rosebrock December 10, 2016 at 7:24 am #

      Hey Nick — did you forget to run make install? It’s normal to see in your build directory prior to actually running make install.

  7. Karen December 8, 2016 at 10:51 pm #

    Thanks so much for your extremely helpful tutorial, I’ve been hoping you posted this blog for weeks! Just have one final question when I try to verify OpenCV 3 install on my macOS (Sierra), when I open up python3 IDLE and try to import cv2 function, I get error message “No module named CV2”. How do I go about using opencv3 module?

    • Adrian Rosebrock December 10, 2016 at 7:14 am #

      Are you using the terminal IDLE or the GUI IDLE? Keep in mind that the GUI version of IDLE does not respect Python virtual environments. You should be using the terminal IDLE instead (and from within the “cv” virtual environment).

  8. Heather December 11, 2016 at 7:15 pm #

    Hi Adrian, thanks so much for creating this site!

    I attempted to follow these instructions exactly, including the part that describes how to construct the CMake command, but am having trouble using functions from opencv_contrib.

    For example:

    Any idea how to fix this?

    • Adrian Rosebrock December 12, 2016 at 10:31 am #

      Double check your OPENCV_EXTRA_MODULES_PATH — it looks like the path you supplied to CMake was incorrect.

      • Heather December 12, 2016 at 9:49 pm #

        Thank you for your response. The path was correct — I checked it a couple times. I suspect the build may have not fully completed although I didn’t see any obvious error messages. In any case, I pulled the latest changes and rebuilt everything (using the exact same CMake command) and this time it worked.

        • Adrian Rosebrock December 14, 2016 at 8:38 am #

          Fantastic, I’m happy to hear it! 🙂

    • Matheus March 14, 2017 at 12:13 pm #

      Hi, Heather

      Any sucess fixing this issue?
      I’ve got the same problem =/

      • Peter April 2, 2017 at 9:47 pm #

        I’m also having the same problem

      • Peter April 3, 2017 at 1:59 am #

        Hi, I just made it work.

        It seems my cmake can’t find opencv_contri unless I trail the path with a ‘/’ (OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules/). Make sense since it’s a folder but even the on GitHub doesn’t do that.

        Also after ‘sudo make install’, the .so in ‘/usr/local/lib/python3.x/site-packages’ will be updated yet does not have any of the opencv_contri modules in it. However in the build folder a just appeared and has all the opencv_contri things…

        • Adrian Rosebrock April 3, 2017 at 1:54 pm #

          The generated file will have your opencv_contrib bindings in it.

  9. Heather December 11, 2016 at 8:28 pm #

    Adrian, one more issue that I’m having trouble troubleshooting: having trouble getting matplotlib to work with the virtual env set up described here. When I attempt “from matplotlib import pyplot as plt” I get:
    RuntimeError: Python is not installed as a framework. The Mac OS X backend will not be able to function correctly if Python is not installed as a framework. See the Python documentation for more information on installing Python as a framework on Mac OS X. Please either reinstall Python as a framework, or try one of the other backends. If you are Working with Matplotlib in a virtual enviroment see ‘Working with Matplotlib in Virtual environments’ in the Matplotlib FAQ

    I checked out the above doc but am still unsure how to proceed. Any pointers?

    I tried specifying “matplotlib.use(“TkAgg”)” and then I no longer get the RuntimeError from the import statement, but when I try to call a matplotlib function, I get:
    libc++abi.dylib: terminating with uncaught exception of type NSException
    Process finished with exit code 134 (interrupted by signal 6: SIGABRT)

    • Adrian Rosebrock December 12, 2016 at 10:29 am #

      This is an issue with matplotlib’s compatibility with Python virtual environments. I’m not sure when the issue will be resolved, but the easiest workaround is to simply install a previous version that is compatible with virtual environments:

      $ pip install matplotlib==1.4.3

      • Heather December 12, 2016 at 10:12 pm #

        Ah, thank you SO much. That was driving me insane.

  10. Rob B December 13, 2016 at 2:17 pm #

    Thank you SO much!! I never would have got this working without your tutorial!

    • Adrian Rosebrock December 14, 2016 at 8:29 am #

      No problem Rob, I’m happy I could help.

  11. chunwai December 16, 2016 at 10:01 pm #

    Hi Adrian, I follow exactly the same steps but Terminal return (-bash: pip: command not found) when I try to install virtualenv and virtualenvwrapper by this command in step 4: pip install virtualenv virtualenvwrapper. However, if I use pip3, it works.

    • Adrian Rosebrock December 18, 2016 at 8:41 am #

      You can install virtualenv and virtualenvwrapper via pip3, that should work just fine. Although if you do, you might need to manually set the VIRTUALENVPATH. You should refer to the virtualenv and virtualenvwrapper documentation for more information on this.

      • Oliver Obst January 7, 2017 at 4:15 am #

        I also needed to set python3 for virtualenvwrapper, it is trying to use python otherwise.

        # Virtualenv/VirtualenvWrapper
        export VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python3

        • Adrian Rosebrock January 7, 2017 at 9:22 am #

          Thanks for sharing Oliver!

        • haley April 11, 2017 at 10:11 am #

          Thanks this helped.

      • Nishatul Majid January 15, 2017 at 7:01 am #

        Hi, how do I manually set the VIRTUALENVPATH? Thanks.

        • Adrian Rosebrock January 15, 2017 at 12:01 pm #

          What do you mean by “manually” set it? You can use the “export” command as mentioned above.

  12. Andre C December 18, 2016 at 9:47 pm #

    After updating to Sierra your tutorial worked well on my mac. Thank you!

    • Adrian Rosebrock December 21, 2016 at 10:42 am #

      Glad to hear it Andre! 🙂

  13. Matthew Mella December 22, 2016 at 3:13 am #

    actually just how do I delete the build directory because I used yyy? thank you

    • Adrian Rosebrock December 23, 2016 at 10:55 am #

      Assuming you are in the build directory now, just go up one directory and use the rm -r command:

  14. Son Le December 24, 2016 at 9:28 am #

    If you encounter this error: “command not found: workon”, follow these steps:

    $ export WORKON_HOME=~/.virtualenvs
    $ VIRTUALENVWRAPPER_PYTHON=’/usr/bin/python3′ # or whenever your python3 is
    $ source /usr/local/bin/

  15. Jurgis December 25, 2016 at 10:11 am #

    Hey Adrian thanks for the tut,

    I was fiddling around for some time, carefully following your guide and everything would would until the Cmake part. it compiled everything correctly, but couldnt detect the python 3 libraries, so i went to the internet to search for alternative ways and found this website and tried the authors code :

    it seems to have worked and detected the python libraries, i’ll try finishing your tutorial and will see how it goes!


    • Adrian Rosebrock December 31, 2016 at 1:44 pm #

      Awesome, thank you for sharing Jurgis!

  16. Anura January 9, 2017 at 11:26 pm #

    Hi Adrian,

    By the time I was trying this tutorial, Python is in version 3.6. At Step 10 to verify OpenCV3 install, nothing appears after the command import cv2. Nothing appeared out of the ordinary in earlier steps. Any clue ?

    • Adrian Rosebrock January 10, 2017 at 1:05 pm #

      What do you mean by “nothing appears after the command import cv2”? Does Python crash? Do you get an error message of some kind?

      • Anura January 10, 2017 at 7:24 pm #


        Apologies. My bad. It does work. I was expecting cv2.__version__ to be an output when in fact it was a command. When I checked…it’s at ‘3.2.0-dev’

        • Adrian Rosebrock January 11, 2017 at 10:32 am #

          No problem Anura, I’m happy we could resolve the problem 🙂

  17. Judah January 10, 2017 at 12:43 pm #

    Thank you for this easy to follow guide!

  18. Mike January 13, 2017 at 11:58 am #

    Hi Adrian,

    How would I go about uninstalling OpenCV if I installed it via this method?

    • Adrian Rosebrock January 15, 2017 at 12:11 pm #

      Remove all OpenCV-related files (you’ll see OpenCV in the filename) from /usr/local/lib.

  19. Victor January 17, 2017 at 6:44 pm #

    Thanks for your tutorial! I think I managed to get it installed and am looking forward to follow the rest of your tutorials. Cheers!

    • Adrian Rosebrock January 18, 2017 at 7:11 am #

      Great job victor, congrats! 🙂

  20. Mike January 26, 2017 at 9:34 am #

    I run as a non-admin user on OSX. Does this install work for that? I temporarily run as root in the terminal when setting things like this up, as you almost always need root access. After setting up as root, will everything work for my standard user account?

    • Adrian Rosebrock January 28, 2017 at 7:00 am #

      Once you compile and install OpenCV you will no longer need root access.

  21. Tadhg Riordan January 27, 2017 at 10:07 pm #

    Can confirm working for python 3.6 with some slight directory changes for the cmake command. Thank you! 🙂

    • Adrian Rosebrock January 28, 2017 at 6:44 am #

      This tutorial will work with Python 3.6 as long as you update the CMake command to point to your Python 3.6 files.

  22. Tom Lorent Bourdo February 2, 2017 at 2:53 am #

    Hey, do you have any ideas on how could I do to link my python openCV with anaconda not to work with the console anymore ? thanks for the great work !

    • Adrian Rosebrock February 3, 2017 at 11:19 am #

      What do you mean by “not to work with the console anymore”?

  23. Harsha February 4, 2017 at 10:58 pm #

    Hi Adrian, I have installed Opencv for python 2.7 using your post( It worked great. Currently I also want to install opencv for python3. How can I install it ? as in do I need to compile using a fresh opencv download to a folder different than ~/opencv ?

    Thanks in advance.

    • Adrian Rosebrock February 7, 2017 at 9:23 am #

      You would need to create a new “build” directory, create a Python virtual environment for Python 3, run CMake, compile OpenCV, and then move the file into your site-packages directory.

  24. Chandramauli Kaushik February 6, 2017 at 5:32 am #

    Thank you so much, I finally got OpenCV with python3.6.
    Great page and tutorials.
    Thanks :)))

    • Adrian Rosebrock February 7, 2017 at 9:14 am #

      Great job Chandramauli!

  25. Nome February 10, 2017 at 10:26 pm #

    Thanks for the great tutorial! I followed it and was able to get OpenCV working, until I encountered a strange error. I had been able to use VideoCapture no problem until I recently transferred everything over to a new hard drive. I copied over everything so it should ostensibly work the same. However, now OpenCV is unable to use VideoCapture and returns error: (-215). It sounds like it could be a ffmpeg issue, but any ideas how to fix it if I followed your installation tutorial?


    • Adrian Rosebrock February 13, 2017 at 1:52 pm #

      That is quite strange, but since you’re on new hardware you might want to consider simply re-compiling OpenCV. That would be my main suggestion to resolve the issue.

      • Nome February 21, 2017 at 12:51 am #

        Not sure what went wrong, but sure enough re-compiling did the trick!

        • Adrian Rosebrock February 22, 2017 at 1:41 pm #

          Fantastic, I’m happy to hear re-compiling worked!

  26. Rana February 15, 2017 at 8:43 pm #

    is there a way to make it work without virtual environment?

    • Adrian Rosebrock February 16, 2017 at 9:49 am #

      Are you trying to compile and install OpenCV without a virtual environment? Or access the OpenCV bindings from outside the virtual environment?

  27. Marko February 26, 2017 at 6:13 pm #

    Hi, thanks. Couple of questions:

    1. At the end of step 7, you say, “As an example, take a look at the Python 3 section output from my configuration:” – what is the command to access these config settings?

    2. In what directory do I run the “make -j4” command?

    • Adrian Rosebrock February 27, 2017 at 11:09 am #

      1. Take a look at the output of “CMake”.

      2. You should run the make -j4 command in the build directory after CMake has (successfully) ran.

  28. Antonios February 26, 2017 at 9:51 pm #

    Hi Adrian.
    In order to follow your way of working with Python & OpenCV on my MAC,
    on step 2 – I installed & builded the nano text editor.
    Then , you say “Save & exitor the editor followed by source …”.
    My question is how to save ?
    In case that I press (cmd + S), it will guide me to Save a something.txt file in the Documents file.


  29. Antonios February 27, 2017 at 12:29 am #

    Hi Adrian.
    On step8 – you type $ make -j4, in order to run for all 4 cores,
    but in my case I have 2 core system. Should I run $ make -j2 ?


    • Adrian Rosebrock February 27, 2017 at 11:07 am #

      Correct, if you only have 2 cores on your system you should run make -j2.

  30. Antonios Kats February 27, 2017 at 5:23 am #

    Hi Adrian, thank you very much for this tutorial.
    I completed it and I’m so happy about it. I’ d like to ask you what would be the next step.
    I tried to create a new python file by texting $ nano (in the cv environment from the step#10, but didn’t work. I need your help 😉

    • Adrian Rosebrock February 27, 2017 at 11:05 am #

      I’m not sure what you mean by “it didn’t work”. Did you receive an error message of some kind?

      • Antonios Kats March 2, 2017 at 6:58 pm #

        I just needed to install scipy in the working environment (cv).
        Thanks for the help !

  31. Sjoerd March 1, 2017 at 10:04 am #

    Ok, so first of all: thnx for this amazing tutorial and the time you obviously devoted to it!

    For me the worst challenge was to get OpenCV installed for my native python3.4 install on macOS 10.12.3 (there is a tutorial for this on your website as well, but it uses Homebrew which in turn automatically installs Python3.6 while I really need 3.4 for other reasons). But anyways, got that solved.

    Then some small adaptions where needed but those are already mentioned in the earlier comments.

    One thing though: when applying your code to my own dataset a lot of the ‘results’ where to big to display on my screen, so only parts of them where visible. I solved this by changing the last few lines of code in to:

    for (score, resultID) in results:
    # load the result image and display it
    result = cv2.imread(args[“result_path”] + “/” + resultID)
    resultresized = cv2.resize(result, (600,600))
    cv2.imshow(“Result”, resultresized)

    This will allow you to resize the outfit to fit your screen. I used 600 by 600 pixels as an example but off course one could also write a function to downscale the output to a minimum size while maintaining the original proportions.

    • Adrian Rosebrock March 2, 2017 at 6:48 am #

      Thanks for the comment Sjoerd. Out of curiosity, which images were you trying to display that were too big to display to your screen? I normally try to keep the result images smaller for this reason.

  32. Thibault March 5, 2017 at 2:59 am #

    I installed it on 3.6
    I used the following cmake and it worked properly. you have to adjust it with your setup eventually.

    it worked well.
    The file actually was installed directly in the virtual env with the name:

    • Misha March 14, 2017 at 9:22 am #

      Wow your version worked on my Mac with 3.6 as well, while Adrian’s failed(

      Thank you so much, Thibault!!!

      • Adrian Rosebrock March 15, 2017 at 8:53 am #

        Thank you for sharing Thibault and Misha! I will create another tutorial for Python 3.6 + OpenCV 3 in the future.

    • Simon April 5, 2017 at 12:52 pm #

      This worked for me on my MAC, thank you!

  33. nick March 8, 2017 at 2:53 am #


    but I just come across two question…
    1, I don’t have a python file in the Cellar (I used my own python3.5 without using homebrew) .
    2, after cmake, the config is succesufll but the Python3’s libraries parts show YYY rather than a path…

    Can you help me fix it? Thanks a lot!

    • Adrian Rosebrock March 8, 2017 at 1:02 pm #

      You need to replace “YYY” with the actual path to your Python library.

  34. Jonny Taylor March 21, 2017 at 8:21 am #


    Thanks for writing this tutorial. For future reference, a note about what I needed to do (Anaconda python 3 on OS X 10.9.5):

    When building with the tagged 3.2.0 build I encountered the following error during the ‘make’ phase:

    In file included from /Volumes/Development/opencv/modules/core/src/hal_internal.cpp:49:
    /Volumes/Development/opencv/build/opencv_lapack.h:1:10: fatal error: ‘CBLAS_H_PATH-NOTFOUND/Accelerate/Accelerate.h’ file not found
    #include “CBLAS_H_PATH-NOTFOUND/Accelerate/Accelerate.h”

    I could not see any obvious error at the cmake stage (output here

    Instead, I tried building against the latest ‘master’ build for opencv and opencv_contrib (3.2.0-332-gc1007c7). When doing this, I encountered the following problem (; the instruction to disable freetype solved that problem and I was able to compile successfully.

  35. Lakshman March 27, 2017 at 8:13 am #

    I completed the installation, and the file is copied to /Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages as When i try to use the package by using import cv2, it is giving

    import cv2
    ImportError: dynamic module does not define module export function (PyInit_cv2)

    Please help me out, how to resolve the issue

    Environment and OS details below
    OS: macOs – 10.11.6
    Python: 3.5

    • Adrian Rosebrock March 28, 2017 at 1:02 pm #

      It sounds like CMake might have compiled OpenCV with Python 2.7 support rather than Python 3. Go back to the CMake step and check which bindings are being compiled.

  36. Ellery March 30, 2017 at 12:00 pm #

    I get this warning when I run brew linkapps. Should I be worried?

    $ brew linkapps python3
    Warning: brew linkapps has been deprecated and will eventually be removed!

    • Adrian Rosebrock March 31, 2017 at 1:51 pm #

      Nope, everything is okay. You can ignore the warning.

  37. Subhaya Tuladhar April 5, 2017 at 8:17 am #

    Will this also work for python 3.6?

    • Adrian Rosebrock April 5, 2017 at 11:48 am #

      Yes, simply update all Python 3.5 paths to be valid Python 3.6 paths.

  38. Mohamad April 27, 2017 at 4:38 am #

    Hey Adrian,

    I followed your instructions but there is a problem with my Cmake output. When it’s finished, I checked the python3 libraries path and it haven’t found the library’s path. As the below:
    Python 3:
    — Interpreter: /usr/local/bin/python3 (ver 3.6.1)
    — Libraries: NO
    — numpy: /usr/local/lib/python3.6/site-packages/numpy/core/include (ver 1.12.0)
    — packages path: /usr/local/lib/python3.6/site-packages

    After that, when I run make -j8 , it gives me errors. How can I solve it?

    • Adrian Rosebrock April 28, 2017 at 9:35 am #

      Either your PYTHON3_LIBRARY or PYTHON3_INCLUDE_DIR is incorrect. Double-check them per the instructions I have included in this guide.

  39. Yash April 27, 2017 at 3:31 pm #

    Hello Adrian,

    When I am trying to use the command ‘make-j4’ command, I get this error.

    “make: *** No targets specified and no makefile found. Stop.”

    What can I do??

    • Adrian Rosebrock April 28, 2017 at 9:27 am #

      Your CMake command likely exited with an error. Go back to the CMake step, look for the error, and then resolve it. The CMake command is used to generate the Makefile required to build OpenCV.


  1. Install OpenCV 3 on macOS with Homebrew (the easy way) - PyImageSearch - December 19, 2016

    […] the past few weeks I have demonstrated how to compile OpenCV 3 on macOS with Python (2.7, 3.5) bindings from […]

Leave a Reply