macOS: Install OpenCV 3 and Python 2.7

sierra_os_hog_example

I’ll admit it: Compiling and installing OpenCV 3 on macOS Sierra was a lot more of a challenge than I thought it would be, even for someone who has a compiled OpenCV on hundreds of machines over his lifetime.

If you’ve tried to use one of my previous tutorials on installing OpenCV on your freshly updated Mac (Sierra or greater) you likely ran into a few errors, specifically with the QTKit.h  header files.

And even if you were able to resolve the QTKit problem, you likely ran into more issues trying to get your CMake command configured just right.

In order to help resolve any issues, problems, or confusion when installing OpenCV with Python bindings on macOS Sierra (or greater) I’ve decided to create two hyper-detailed tutorials:

  1. This first tutorial covers how to install OpenCV 3 with Python 2.7 bindings on macOS.
  2. My second tutorial will come next week where I’ll demonstrate how to install OpenCV 3 with Python 3.5 bindings on macOS.

I decided to break these tutorials into two separate blog posts because they are quite lengthy.

Furthermore, tuning your CMake command to get it exactly right can be a bit of a challenge, especially if you’re new to compiling from OpenCV from source, so I wanted to take the time to devise a foolproof method to help readers get OpenCV installed on macOS.

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

macOS: Install OpenCV 3 and Python 2.7

The first part of this blog post details why I am creating a new tutorial for installing OpenCV 3 with Python bindings on the Mac Operating System. In particular, I explain a common error you may have run across — the QTKit.h  header issue from the now deprecated QTKit library.

From there, I provide super detailed instructions on how to install OpenCV 3 + Python 2.7 your macOS Sierra system or greater.

Avoiding the QTKit/QTKit.h file not found error

In the Mac OSX environment the QTKit (QuickTime Kit) Objective-C framework is used for manipulating, reading, and writing media. In OSX version 10.9 (Mavericks) QTKit was deprecated (source).

However, it wasn’t until the release of macOS Sierra that much of QTKit was removed and instead replaced with AVFoundation, the successor to QTKit. AVFoundation is the new framework for working with audiovisual media in iOS and macOS.

This created a big problem when compiling OpenCV on Mac systems — the QTKit headers were not found on the system and were expected to exist.

Thus, if you tried to compile OpenCV on your Mac using my previous tutorials your compile likely bombed out and you ended up with an error message similar to this:

Even more problematic, both the tagged releases of OpenCV v3.0 and v3.1 do not include fixes to this issue.

That said, the latest commits to the OpenCV GitHub repo do address this issue; however, a new tagged release of v3.2 has yet to be released.

That said, I’m happy to report that by using the latest commit to OpenCV’s GitHub we can install OpenCV on macOS Sierra and greater.

The trick is that we need to use the HEAD  of the repo as opposed to a tagged release.

Once OpenCV 3.2 is released I’m sure the QKit to AVFoundation migration will be included, but until then, if you want to install OpenCV 3 on your macOS system running Sierra or later, you’ll need to avoid using tagged releases and instead compile and install the development version of OpenCV 3.

How do I check my Mac Operating System version?

To check your Mac OS version click the Apple icon at the very top-left corner of your screen in the menu then select “About this Mac”.

A window should then pop up, similar to the one below:

Figure 1: Checking your OS version on Mac. My machine is currently running macOS Sierra (10.12).

Figure 1: Checking your OS version on Mac. My machine is currently running macOS Sierra (10.12).

If you are running macOS Sierra or greater, you can use this tutorial to help you install OpenCV 3 with Python 2.7 bindings.

If you are using an older version of the Mac Operating System (Mavericks, Yosemite, etc.), please refer to my previous tutorials.

Step #1: Install Xcode

Before we can even think about compiling OpenCV, we first need to install Xcode, a full blown set of software development tools for the Mac Operating System.

Register for an Apple Developer account

Before downloading Xcode you’ll want to register with the Apple Developer Program (it’s free). If you have an existing Apple ID (i.e., what you use to sign in to iTunes with) this is even easier. Simply provide some basic information such as name, address, etc. and you’ll be all set.

From there, the easiest way to download Xcode is via the App Store. Search for “Xcode” in the search bar, select it, and then click the “Get” button:

Figure 2: Selecting Xcode from the Apple App Store.

Figure 2: Selecting Xcode from the Apple App Store.

Xcode will then start to download and install. On my machine the download and install process took approximately 30 minutes.

Accept the Apple Developer license

Assuming this is the first time you’ve installed or used Xcode, you’ll need to accept the developer license (otherwise, you can skip this step). I prefer using the terminal whenever possible. You can use the following command to accept the Apple Developer License:

Scroll to the bottom of the license and accept it.

Install Apple Command Line Tools

Finally, we need to install the command line tools. These tools include packages such as make, GCC, clang, etc. This is absolutely a required step, so make sure you install the command line tools:

After you enter the command above a window will pop up confirming that you want to install the command line tools:

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

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

Click “Install” and the Apple Command Line Tools will be downloaded and installed on your system. This should take less than 5 minutes.

Step #2: Install Homebrew

We are now ready to install Homebrew, a package manager for macOS. Think of Homebrew as similar equivalent to apt-get for Ubuntu and Debian-based systems.

Installing Homebrew is simple. Simply copy and paste the command underneath the “Install Homebrew” section of the Homebrew website (make sure you copy and paste the entire command) into your terminal:

Once Homebrew is installed you should update it to ensure the most recent package definitions are downloaded:

The last step is to update our ~/.bash_profile  file. This file may exist on your system already or it may not. In either case, open it with your favorite text editor (I’ll use nano  in this case):

And insert the following lines at the bottom of the file (if ~/.bash_profile  does not exist the file will be empty, so simply insert the following lines):

The above snippet updates your PATH  variable to look for binaries/libraries along the Homebrew path before searching your system path.

After updating the file save and exit the editor. I have included a screenshot of my ~/.bash_profile  below:

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

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

You should then use the source  command to ensure the changes to your ~/.bash_profile  file are manually reloaded:

This command only needs to be executed once. Whenever you login, open up a new terminal, etc., your .bash_profile  will will automatically be loaded and sourced for you.

Step #3: Setup Homebrew for Python 2.7 and macOS

In general, you do not want to develop against the system Python as your main interpreter. This is considered bad form. The system version of Python should (in an ideal world) serve only one purpose — support system operations.

Instead, you’ll want to install your own version of Python that is independent of the system one. Installing Python via Homebrew is dead simple:

Note: This tutorial covers how to install OpenCV 3 with Python 2.7 bindings on macOS. Next week I’ll be covering OpenCV 3 with Python 3 bindings — if you want to use Python 3 with OpenCV on macOS, please refer to next week’s blog post.

After the install command finishes we just need to run the following command to complete the Python installation:

To confirm that we are using the Homebrew version of Python rather than the system version of Python you should use the which  command:

Important: Be sure to inspect the output of the which  command! If you see /usr/local/bin/python  then you are correctly using the Hombrew version of Python.

However, if the output is /usr/bin/python  then you are incorrectly using the system version of Python. If this is the case then you should ensure:

  1. Homebrew installed without error.
  2. The brew install python  command completed successfully.
  3. You have properly updated your ~/.bash_profile  file and reloaded the changes using source . This basically boils down to making sure your ~/.bash_profile  looks like mine above in Figure 4.

Step #4: Install virtualenv, virtualenvwrapper, and NumPy

We are now ready to install three Python packages: virtualenv and virtualenvwrapper, along with NumPy, used for numerical processing.

Installing virtualenv and virtualenvwrapper

The virtualenv  and virtualenvwrapper  packages allow us to create separate, independent Python environments for each project we are working on. I’ve mentioned Python virtual environments many times before, so I won’t rehash what’s already been said. Instead, if you are unfamiliar with Python virtual environments, how they work, and why we use them, please refer to the first half of this blog post. There is also an excellent tutorial on the RealPython.com blog that takes a deeper dive into Python virtual environments.

To install virtualenv  and virtualenvwrapper , just use pip :

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

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

Figure 5: Update your .bash_profile file to include virtualenv/virtualenvwrapper.

Figure 5: Update your .bash_profile file to include virtualenv/virtualenvwrapper.

Save and exit your text editor, followed by refreshing your environment using the source  command:

Again, this command only needs to be executed once. Whenever you open up a new terminal the contents of your .bash_profile  file will be automatically loaded for you.

Creating your Python virtual environment

Assuming the above commands completed without error, we can now use the mkvirtualenv  command to create our Python virtual environment. We’ll name this Python virtual environent  cv :

This command will create a Python environment that is independent from all other Python environments on the system (meaning this environment has its own separate site-packages  directory, etc.). This is the virtual environment we will be using when compiling and installing OpenCV.

The mkvirtualenv  command only needs to be executed once. If you ever need to access this virtual environment again, just use the workon  command:

To validate that you are in the cv  virtual environment, simply examine your command line — if you see the text (cv)  preceding the prompt, then you 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 6: 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 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.

To access the cv  virtual environment simply use the workon  command mentioned above.

Install NumPy

The last step is to install NumPy, a scientific computing package for Python.

Ensure you are in the cv  virtual environment (otherwise NumPy will be installed into the system version of Python rather than the cv  environment) and then install NumPy using pip :

Step #5: Install OpenCV prerequisites using Homebrew

OpenCV requires a number of prerequisites, all of which can be installed easily using Homebrew.

Some of these packages are related to tools used to actually build and compile OpenCV while others are used for image I/O operations (i.e., loading various image file formats such as JPEG, PNG, TIFF, etc.)

To install the required prerequisites for OpenCV on macOS, just execute these commands:

Step #6: Download the OpenCV 3 source from GitHub

As I mentioned at the top of this tutorial, we need to compile OpenCV from the latest commit, not a tagged release. This requires us to download the OpenCV GitHub repo:

Along with the opencv_contrib repo:

Step #7: Configuring OpenCV 3 and Python 2.7 via CMake on macOS

In this section I detail how to configure your OpenCV 3 + Python 2.7 on macOS Sierra build using CMake.

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

I then provide a CMake build template that you can use. This template requires you to fill in two values — the path to your libpython2.7.dylib  file and the path to your Python.h  headers.

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

Finally, I provide an example of a fully completed CMake command. However, please take note that this 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 more details.

Setting up the build

In order to compile OpenCV 3 with Python 2.7 support for macOS we need to first set up the build. This simply amounts to changing directories into opencv  and creating a build  directory:

OpenCV 3 + Python 2.7 CMake template for macOS

In order to make the compile and install process easier, I have constructed the following template OpenCV 3 + Python 2.7 CMake template:

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

  1. BUILD_opencv_python2=ON : This indicates that we want to build Python 2.7 bindings for our OpenCV 3 install.
  2. BUILD_opencv_python3=OFF : Since we are compiling Python 2.7 bindings we need to explicitly state that we do not want Python 3 bindings. Failure to include these two switches can cause problems in the CMake configuration process.
  3. PYTHON2_LIBRARY=YYY : This is the first value you need to fill in yourself. You will need to replace YYY  with the path to your libpython2.7.dylib  file (I will help you find it in the next section).
  4. PYTHON2_INCLUDE_DIR : This is the second value you will need to fill in. You need to replace ZZZ  with the path to your Python.h  headers (again, I’ll help you determine this path).

Determining your Python 2.7 library and include directory

Let’s start by configuring your PYTHON2_LIBRARY  value. This switch should point to our libpython2.7.dylib  file. You can find this file within many nested subdirectories of /usr/local/Cellar/python/ . To find the exact path to the libpython2.7.dylib  file, just use the ls  command along with the wildcard asterisk:

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

Next, let’s determine the PYTHON2_INCLUDE_DIR . This path should point to the Python.h  headers used to generate our actual OpenCV + Python 2.7 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 you’ve determined the PYTHON2_LIBRARY  and PYTHON2_INCLUDE_DIR  values you need to update the CMake command with these values.

On my particular machine the full CMake command looks like this:

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

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

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

Figure 8: Ensuring that Python 2.7 will be used when compiling OpenCV 3 for macOS.

Figure 8: Ensuring that Python 2.7 will be used when compiling OpenCV 3 for macOS.

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 libpython2.7.dylib  file.
  3. The numpy  version being utilized is the one you installed in your cv  virtual environment.

Step #8: Compile and install OpenCV on macOS

Assuming you cmake  command exited without error and your Python 2  section is properly configured, you can now compile OpenCV:

The -j  switch controls the number of parallel processes to compile OpenCV. We normally set this to the number of available cores/processors on our machine. Since I’m on a quad-core system, I use -j4 .

OpenCV can take awhile to compile (30-90 minutes) depending on the speed of your machine. 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 9: Successfully compiling OpenCV 3 from source with Python 2.7 bindings on macOS.

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

Step #9: Sym-link your OpenCV 3 + Python 2.7 bindings

After running make install  you should now see a file named cv2.so  in /usr/local/lib/python2.7/site-packages :

The cv2.so  file is your actual set of OpenCV 3 + Python 2.7 bindings.

However, we need to sym-link these bindings into our cv  virtual environment. This can be accomplished using the following commands:

Step #10: Testing your OpenCV install on macOS

To verify that your OpenCV 3 + Python 2.7 installation on macOS is working:

  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 to test the install process:

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 2.7 shell on macOS Sierra:

Figure 10: Ensuring that I can successfully import my OpenCV + Python bindings on macOS.

Figure 10: Ensuring that I can successfully import my OpenCV 3 + Python 2.7 bindings on macOS.

Congratulations, you have installed OpenCV 3 with Python 2.7 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.

curious_about_cv

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.

Summary

In this blog post I demonstrated how to install OpenCV 3 with Python 2.7 bindings on macOS Sierra and above.

Next week I’ll have a second tutorial, this one covering OpenCV 3 with Python 3.5 bindings on macOS.

For more OpenCV install tutorials on other operating systems (such as Ubuntu, Raspbian, etc.), please refer to this page where I provide additional links and resources.

But before you go

If you’re interested in learning more about OpenCV, computer vision, and image processing be sure to enter your email address in the form below to be notified when new blog posts + tutorials are published!

, , , ,

191 Responses to macOS: Install OpenCV 3 and Python 2.7

  1. George Profenza November 28, 2016 at 12:03 pm #

    OMG! Sooooo frustratingly long-winded with Sierra. Still sticking to 10.11.5 for now.
    Thank you for sparing us the headaches Adrian

    • Adrian Rosebrock November 28, 2016 at 2:40 pm #

      It’s a pain, but it’s getting easier. It will get much easier once OpenCV 3.2 is released.

  2. Mark November 28, 2016 at 12:12 pm #

    Worked like a charm!

    • Adrian Rosebrock November 28, 2016 at 2:41 pm #

      Glad to hear it Mark, congrats on getting OpenCV installed on Sierra!

  3. A K November 29, 2016 at 1:48 am #

    Thank you for this great tutorial! This is exactly what I was searching on your blog during the Thanksgiving break ….looks like an early Christmas gift. Looking forward to next week’s Python 3.5 tutorial.

    • Adrian Rosebrock November 29, 2016 at 7:56 am #

      Thank you for the kind words, comments like these make it worth writing these blog posts 🙂

  4. Claudiu November 29, 2016 at 10:46 pm #

    I’ve had problems with step #7 for some reason it wouldn’t have the correct output like your screenshots. I found a github issue where other people had that and wanted to mention it that I just had to delete the build directory and run cmake again about 3-4 times until it gave me the right results (if anyone else runs into that problem)

    Besides that everything else worked perfectly, thank you again!

    • Monkey December 7, 2016 at 5:33 am #

      Thanks for the suggestion, I came across the same problem and now fixed it

  5. Yoni Fihrer November 29, 2016 at 11:10 pm #

    Thank you for this fix!!!
    During step 7, I get the specific path for libpython2.7.dylib and use that for the cmake, but it still come back at YYY. (I did it once without finding the correct filepath)
    Thanks again

    • Adrian Rosebrock December 1, 2016 at 7:42 am #

      It sounds like you might be copying and pasting the template into the command line rather than the updated path. If you’re still getting an issue try deleting the build directory, re-creating, and then re-running the CMake command with the paths updated.

  6. Bill Rodriguez November 30, 2016 at 2:13 pm #

    Adrian, I am pretty sure I followed all the steps properly and in my Cmake list I have python2=ON and python3=OFF as you stated. However when I run the Cmake list the error comes up that I need python3. What silly mistake have I made?

    Thanks,
    Bill

    • Adrian Rosebrock December 1, 2016 at 7:29 am #

      Hey Bill, what do you mean by a CMake error that lists you “need” Python 3? The sections you should be most concerned about are the “Python 2” and “Python 3” output sections of CMake that I have included in Figure 8.

  7. Schi December 4, 2016 at 4:43 am #

    Hey Adrian,

    short question, what are your reasons to use pip & virtuellenv vs anaconda? Friends of mine using anaconda for an virtuell environment and thought I also want to give it a try, but as I’m a noob, I’m afraid I won’t be able to do execute your instructions and using anaconda. So any thoughts about why you don’t use it?

    And also what would be your recommendation to a total beginner?

    Thanks,
    Schi

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

      Personally, I don’t like the Continuum company. A few years ago I had a pretty big falling out with them over business I would rather not discuss. There is nothing wrong with Anaconda, it’s a good deal — but I won’t recommend using it. I personally think virtualenv is easier to use and gives you more control in the long run.

  8. Tom de Ruiter December 4, 2016 at 7:13 pm #

    Hey! Great work, kudos!

    It seems I get stuck on step #7. Here I get the following response that Python2 has no libraries. It literally has a parameter ‘NO’ where normally the path would have been. Also, python2 is included in the unavailable array in the opencv modules.

    Any idea how to fix?

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

      Hey Tom — if the parameter says “NO” then you do not have the correct path for the Library. You’ll want to double check your libpython2.7.dylib path. Also make sure you are in the “cv” virtual environment prior to executing CMake.

      • Tom de Ruiter December 5, 2016 at 5:47 pm #

        Aha, I checked it a dozen times but couldn’t figure it out. Turned out that I messed up the virtual environments and used the old cmake parameters which were not initialized good. After reinstalling and deleting all environments I had a successful install! Thanks!

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

          Congrats on resolving the issue Tom, nice job! 🙂

        • Pedro Martins December 15, 2016 at 2:50 pm #

          I felt into the same problem on step #7 and couldn’t figure out how to solve it. To reinstall the cv virtual environment was the solution. Thanks Tom!

        • Mehmet Aydemir February 24, 2017 at 8:16 pm #

          I have the same problem, can i learn how do you do that? I messed up

    • Xiao December 6, 2016 at 3:20 pm #

      I followed the step 7 and I got the same results with the compiling output showing that NO for the libraries… I check my path for the file multiple times and I was in the cv environment to execute the command. Do you happen to know what could be the problem?

      UPDATE:

      I solved my problem by adding sudo … Thank you for making the post. It is really useful.

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

        You needed to add “sudo” to the CMake command? That doesn’t seem right. I would be curious to know more about any other commands you executed or if you are compiling OpenCV on a shared system of some sort.

  9. LibingMao December 6, 2016 at 4:11 am #

    Hey,Adrian.
    Great work.During step 7,i use my own file path replace yours.I have no errer in cmake but when i run make -j4 i still get the QTKit error.I double check the results from the cmake. The python2 is the same as yours.So i am confused now.

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

      You’re still getting the QTKit error? Can you confirm that you are using macOS Sierra? Are you using a fresh install of Sierra? Or an upgraded version?

      • LibingMao December 10, 2016 at 11:07 pm #

        My Sierra version is 10.12.1.

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

          Did you upgrade to Sierra? Or is this a fresh install of Sierra? If you upgraded, I would wipe the drive and start fresh.

          • yasir February 1, 2017 at 12:57 pm #

            I’ am still getting the qkit error in Sierra 10.12.2 (16C68)

          • Adrian Rosebrock February 1, 2017 at 1:03 pm #

            Try compiling and installing OpenCV from a fresh install of macOS Sierra on your system. I can’t be sure, but if you’ve upgraded your system multiple times you may have some legacy cruft on your system causing the error (that is my best guess).

  10. Jacob December 7, 2016 at 2:30 am #

    So is the best solution for OSX users to avoid the QTKit problem (I’m on El Capitan) to update to Sierra and follow this tutorial?

    Thanks!

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

      El Capitan is sort of in “no mans land”. You are between Yosemite and Sierra, and unfortunately it’s hard to tell what state your system could be in. I would either downgrade to Yosemite or upgrade to Sierra.

  11. rick December 12, 2016 at 7:07 pm #

    I for the life of me cannot get the cmake template output to match all three requirements for python 2:

    1. The Interpreter points to the Python binary in your cv virtual environment.
    — check
    2. Libraries points to your libpython2.7.dylib file.
    –nope
    3. The numpy version being utilized is the one you installed in your cv virtual environment.
    –nope

    nor is it even listing my packages path.

    I have tried starting with a fresh and following EXACTLY every step of this guide to no avail.

    Can I just continue on without worrying about it?

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

      The “Libraries” is the most important part. You’ll want to make sure that you have that properly configured.

      • Yue Liu February 22, 2017 at 5:49 pm #

        Hi Adrian, I got same issue. I followed all the steps and put in the library path via using your trick to get YYY and ZZZ, but still got no “Libraries” listed. What could be possibly be the reason? Thanks a lot!

        • Adrian Rosebrock February 24, 2017 at 11:32 am #

          Without physical access to your machine I’m not sure what the exact error could be. I would suggest replacing the library path with $(python-config --prefix) inside the CMake command and seeing if this resolves the issue.

          • Alex February 25, 2017 at 4:32 pm #

            I am having the same behaviour, and nothing before had worked.

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

            If you are having trouble configuring CMake, try to install OpenCV via Homebrew.

          • Fanglin May 17, 2017 at 6:36 pm #

            $(python-config –prefix) works for me. I used your blog solution and got no “Libraries”, but this command can identify it.

          • Adrian Rosebrock May 18, 2017 at 11:50 am #

            Thanks for sharing Fanglin!

  12. Emanuele Palma December 16, 2016 at 4:49 am #

    Hey,Adrian.
    Great work. I follow all the procedures correctly but when I launch make -j4, it gets to 100% and gets stuck to this line here:

    [100%] Built target example_facial_features

    entering a loop of Id mex sh clang. It doesn’t end the make step and so I cannot go on.
    What do you think the problem may be?

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

      This sounds like a race condition of some sort by using 4 threads/cores for compiling. I would do a make clean followed by just make to compile with a single core — that should resolve the issue.

  13. Michael December 17, 2016 at 5:44 pm #

    All steps were ok, until

    Step #9

    In:
    cd /usr/local/lib/python2.7/site-packages/

    there is no cv2.so

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

      Did you forget to run make install? This would account for the lack of cv2.so file. You should also check your CMake output and make sure your Python 2.7 section matches mine, otherwise your OpenCV bindings may have not been built.

      • Jonathan February 20, 2017 at 1:09 am #

        I too was missing the cv2.so file when I came across step #9. My output after completing step 7 for the Python 2 section were mostly “NO” not exactly sure what happened here or where I went wrong. Whats the best fix for this or where should I begin the process over again? Also, if I do need to start from a certain point, does anything need to be deleted before I do so and how?

        Thanks!

        • Adrian Rosebrock February 20, 2017 at 7:39 am #

          You shouldn’t have to start from scratch. I would delete your “build” directory, re-create it, and then re-run CMake. It’s likely that your paths to the Python libraries are incorrect so you’ll need to manually determine them using the “ls” command. Also, make sure you are in the “cv” virtual environment before running the “cmake” command.

        • Alex February 25, 2017 at 4:34 pm #

          The same with me.

    • Bhushan April 24, 2017 at 12:32 pm #

      were you able to resolve this issue ? I am facing the same issue; no “cv2.so” file present. Everything went fine though.

    • TienLe June 27, 2017 at 3:34 am #

      Because you are not in cv virtual environment.
      – Delete the build
      – Use the command $ workon cv
      – Than reinstall opencv

  14. abhijith e m December 18, 2016 at 6:57 am #

    in step 8
    $ make -j4
    make: *** No targets specified and no makefile found. Stop.
    this message occurs why?

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

      This is likely because CMake exited with an error. Go back to the CMake step and check to see if there were any errors.

    • Marc Streeter March 22, 2017 at 9:02 am #

      I had the same problem, and Adrian was right: the CMake had exited in error. In my case, it erred because I had to update the *OpenCV 3 + Python 2.7 CMake template* (displayed below is what it this website has currently at the time of this writing)


      $ cmake -D CMAKE_BUILD_TYPE=RELEASE \
      -D CMAKE_INSTALL_PREFIX=/usr/local \
      -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules \
      -D PYTHON2_LIBRARY=YYY \
      -D PYTHON2_INCLUDE_DIR=ZZZ \
      -D PYTHON2_EXECUTABLE=$VIRTUAL_ENV/bin/python \
      -D BUILD_opencv_python2=ON \
      -D BUILD_opencv_python3=OFF \
      -D INSTALL_PYTHON_EXAMPLES=ON \
      -D INSTALL_C_EXAMPLES=OFF \
      -D BUILD_EXAMPLES=ON ..

      The OPENCV_EXTRA_MODULES_PATH had to be updated to match the path on my machine to the opencv_contrib repo that was cloned in *Step #6: Download the OpenCV 3 source from GitHub*. After that, you should be able to do step 8 again, doing the make -j4 command, on a clean build folder and proceed from there without issue.

      • Adrian Rosebrock March 22, 2017 at 9:09 am #

        I’ll also add that you’ll want to update the template Marc posted above, replacing YYY and ZZZ with the correct library and include paths.

  15. abhijith e m December 19, 2016 at 11:34 am #

    great finally it worked…
    i tried several times using the sourse code provided in opencv.org but it was failure. but yours was perfect thankyouuu….

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

      Nice job getting OpenCV installed! 🙂

  16. Swaroop December 26, 2016 at 12:04 pm #

    I’m getting this error when I’m trying to install virtualenvwrapper, it says the following
    -bash: /usr/local/bin/virtualenvwrapper.sh: No such file or directory
    I followed the same procedure, can you please help me?

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

      It seems likely that you either (1) did not update your .bash_profile file correctly and update it or (2) virtualenv/virtualenvwrapper did not install correctly. I would start by double-checking that these packages are installed via pip freeze.

    • Ishani April 15, 2017 at 1:23 pm #

      I’m facing the same problem. Did you get to a solution?

  17. Héctor January 2, 2017 at 6:59 am #

    One question, in the part “Filling in the CMake template” how can we edit text inside the build directory?

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

      Hey Hector — can you clarify what you mean by “edit text inside the build directory”? I’m not sure what you mean.

    • David44 January 7, 2017 at 2:04 am #

      I think you can use any text editor, such as TextEdit on Mac, to first copy the template, then found the right values for your computer, then use the editor to make the changes. In the end you have a modified Cmake command to be executed from your Mac terminal.

      This has nothing to do with the build directory, it’s just to modify the command so that it has correct folder information so Cmake can be executed correctly.

  18. Jake January 4, 2017 at 8:28 pm #

    Hey man I messed up step 7 and did not fill in the blanks for the template. How do I revert and try again? Run the whole thing from step 1?

    Thanks again, great tutorial!

  19. Sylvain January 5, 2017 at 7:31 am #

    Hey Adrian, thank you very much for this tutorial, it helped me a lot. At step 7: when I copied past your template with my path parameters, I had to add the missing “\” otherwise it didn’t work properly (using python 2.7.13 on Sierra)

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

      Hey Sylvain — where is the missing “\” in the tutorial? I’d be happy to update it.

  20. smather January 7, 2017 at 10:35 am #

    Instead of being loaded from /usr/local/bin/python, which python returns /Library/Frameworks/Python.framework/Versions/2.7/bin/python
    I have the correct config line in the .bash_profile file.
    Any ideas about how to fix this?

    • Adrian Rosebrock January 9, 2017 at 9:19 am #

      Have you ran source ~/.bash_profile after updating the file or opening a new terminal? There may also be a typo in your .bash_profile file.

  21. Lindsay Rawitscher January 11, 2017 at 2:33 pm #

    I am getting stuck on step 7. I believe I have done everything correctly regarding the path names and can’t seem to figure out where I am going wrong. Any help would be appreciated! I have pasted my own pathnames into the template, not copied it directly.

    • Adrian Rosebrock January 12, 2017 at 7:58 am #

      Hey Lindsay — what is the error you are getting? Without knowing your error message myself and other PyImageSearch readers cannot help out.

  22. Brian January 15, 2017 at 3:55 am #

    I am getting stuck on step number 7 as well. Once I execute the cmake command with the proper directories pasted into the template, the Python 2 section in the output only includes information for the interpreter. It says nothing about libraries or numpy. I think I have the same problem as rick did above.

    I tried to proceed anyways, and unsurprisingly it did not work. There is no file named cv2.so in:

    /usr/local/lib/python2.7/site-packages

    I am sure I have followed this tutorial exactly, and this is a completely fresh install of Sierra. Any ideas about what to do?

    • Brian January 15, 2017 at 6:58 pm #

      I solved this by wiping my drive and reinstalling sierra, then trying again from scratch. I still don’t know what went wrong, I did everything the same the second time. At least it works now!

      • Adrian Rosebrock January 16, 2017 at 8:10 am #

        Congrats on resolving the error, Brian! When it comes to installing OpenCV I normally recommend a fresh install of macOS.

    • Chris Gonzales January 24, 2017 at 10:41 pm #

      I had a similar issue to this. The problem is further up in the spew from cmake:

      — Could NOT find PythonLibs: Found unsuitable version “2.7.10”, but required is exact version “2.7.13” (found /usr/lib/libpython2.7.dylib)

      User “Ideoforms” had the correct answer at https://github.com/pybind/pybind11/issues/99 where you have to add the following to your cmake command:

      -DPYTHON_LIBRARY=$(python-config –prefix)/lib/libpython2.7.dylib -DPYTHON_INCLUDE_DIR=$(python-config –prefix)/include/python2.7

      After that, the output looked like it was supposed to. I’m still going through the opencv build, so no guarantees there won’t be any further problems

      • MaximeDi February 3, 2017 at 10:36 am #

        That fixed it for me.
        I just had to write “– prefix” instead of “-prefix”.

        Thank you!

        • Fanglin May 17, 2017 at 6:35 pm #

          Works for me as well.

  23. Conno January 17, 2017 at 5:02 am #

    Hi Adrian, thank you very much for helping us installing OpenCV.
    I perform exactly as you explain steps #1- #3 but then after updating ~/.bash_profile, sourcing it, it still get an output of usr/bin/python for which python. I have tried so many times and I am stuck. What could I try besides doing the steps over and over? Is there a way to delete everything and start over again?

    • Adrian Rosebrock January 17, 2017 at 8:42 am #

      When it comes to installing OpenCV on macOS I recommend a clean, fresh install of the operating system. This ensures there are no legacy paths (perhaps from other libraries you previously installed) that might be causing problems. As far as debugging, I would ensure that the Homebrew Python was actually installed by manually changing directory to /usr/local/bin and ensuring you find the “python” binary there.

    • William March 22, 2017 at 9:50 am #

      The same happened to me. This worked out:

      export PYTHONPATH=”${PYTHONPATH}/usr/local/lib/python2.7/site-packages:/usr/lib/python2.7/site-packages”

      But I not using virtualenv.

      • Adrian Rosebrock March 23, 2017 at 9:34 am #

        Thanks for sharing William, but I will also reiterate on this comment — if you are using a Python virtual environment, don’t use this as it will mess up your Python path inside the environment.

  24. Sidharth Sadani January 17, 2017 at 11:04 pm #

    Hi Adrian,
    Thank you so much for this. Worked in one go perfectly.

    Just one pointer, I got really confused whether I should stay in the cv environment while beginning the OpenCV installation or should I go back to the main env. Before reading your post till the end, and I know I might sound like a novice, which I am at OpenCV, I did not know that there are bindings established between OpenCV and python during installation. So you might just want to reiterate to stay in the ‘cv’ environment at the beginning of the OpenCV installation.

    But otherwise, perfect. I loved it. I’m new to working on a Mac, and I’m sure I would’ve stumbled around a lot to set this up, if not for your post. So thanks for taking the effort.

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

      Thank you for the feedback Sidharth, I appreciate it. I’ll certainly keep this in mind for future OpenCV install tutorials.

  25. Bhavik Patel January 21, 2017 at 2:50 am #

    Just a suggestion. I would strongly encourage you to include warning signs or prompts in your install process for those that already have a version of python installed or use pip instead of homebrew. It can be extremely problematic installing multiple version of python and it nearly impossible to track down install errors.

    • Adrian Rosebrock January 22, 2017 at 10:18 am #

      Hey Bhavik — pip and Homebrew are both unrelated. Homebrew is a library package manager for the OSX/macOS system while pip is strictly for Python packages. As for installing Homebrew via Python, that shouldn’t cause any issues with previous Python versions as long as your PATH variable is properly set.

  26. Darian January 24, 2017 at 12:55 pm #

    Hey Adrian,

    I successfully installed opencv after a few glitches – but when I open up IDLE on my computer and import cv2, I’m getting an error “No module named ‘cv2′”. I thought maybe because I was using a Python 3.5.1 IDLE that this was a problem, but I used a 2.7 version of IDLE as well and no dice. What can I do to use the opencv packages in IDLE?

    Thanks so much.

    • Adrian Rosebrock January 24, 2017 at 2:16 pm #

      I assume you are referring to the GUI version of IDLE? If so, the GUI version does not respect virtual environments. I would suggest using either (1) the command line IDLE or (2) Jupyter Notebooks from within your virtual environments.

  27. Tarek Mansour January 25, 2017 at 10:03 pm #

    Hey Adrian,

    Thanks a lot for this! I am getting stuck on step 7. I followed the instructions pretty well and I got the right path to run cmake but python2 shows only an interpreter field (no libraries or numpy fields). Any insight?

    Thanks!

    • Adrian Rosebrock January 26, 2017 at 8:18 am #

      Hey Tarek — if you’re only getting an Interpreter field then there is an error in your CMake configuration. To resolve the NumPy field, make sure you’re in the “cv” virtual environment an then install NumPy. Regarding the “Libraries” you’ll want to determine this path manually on your machine (yours will likely be different than mine). Use ls and auto-complete to help you determine the correct path.

    • Tarek Mansour January 26, 2017 at 10:52 am #

      Figured it out! The problem was that my ~/.bash_profile had other lines before the homebrew line and this messed it up. The solution was to comment out everything/delete everythin and then add our homebrew lines!

      Thanks again!

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

        Great job resolving the issue Tarek!

  28. Paul January 30, 2017 at 10:28 am #

    Thanks for this guide!
    Was struggling a lot with installing OpenCV for Sierra and this worked extremely smoothly!

    • Adrian Rosebrock January 30, 2017 at 4:18 pm #

      Great job getting OpenCV installed Paul!

  29. Joonas Mankki January 30, 2017 at 1:04 pm #

    Hi Adrian,
    Thanks for the detailed guide! I too got stuck on #7 getting NO Libraries. The issue was that my Homebrew Python was version 2.7.13 but the python in my virtualenv was 2.7.10 so the libraries were not compatible. The cmake-command seems to use libraries from Homebrew but the interpreter from virtualenv so the versions of the two must match. The solution for me was to create the virtualenv using the command “mkvirtualenv -p /usr/local/bin/python cv” to use the same python version as the Homebrew.

    Other than that I had no issues, thanks for taking the time to write this guide!

    • Adrian Rosebrock January 30, 2017 at 4:12 pm #

      Thanks for sharing Joonas! And congrats on getting OpenCV installed.

      • Wingman Shen September 26, 2017 at 8:32 pm #

        Dear Adrian, Thank you so much for your excellent guide and I have now installed cv2 properly on my Mac!

        I run into the same “mismatch” issue as Joonas posted and struggled it for sometime on my own and eventually re-discovered the same solution as he posted. I wish I had seen his post before my struggle.

        To help future readers of your guide, could you please add a note in Step #4 about this mismatch problem and the “-p” solution? Thanks again for your great effort.

        • Adrian Rosebrock September 28, 2017 at 9:21 am #

          Congrats on getting OpenCV installed on your system, Wingman! Nice job!

          I’ll also be doing updated install posts in the future to help resolve this issue.

  30. Chris January 31, 2017 at 4:13 pm #

    As always thanks so much for being an awesome hub for the opencv community. I had a quick question regarding working with different frameworks in conjunction with virtual environments. I am trying to work with the matplotlib library and am able to install it fine using pip and making sure it works. The issue is matplotlib GUI features tend to break when working in a virtual environment. I have looked up some stuff online but haven’t been able to sort it out in your particular build and was wondering if you would have any insight on how best to work around this issue

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

      Hey Chris — what is the specific GUI issue you’re running into with matplotlib?

  31. Andy January 31, 2017 at 7:02 pm #

    At step 7, the output of my cmake command is not showing anything about libraries. It only gives the path to the interpreter (which worked out correctly), but no numpy or library

    • Andy January 31, 2017 at 7:20 pm #

      BTW, I’m sure that numpy was installed in (CV)… any tips?

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

        To ensure NumPy has been installed, access your Python virtual environment and then run “pip freeze”. You should see NumPy in the output if NumPy indeed installed:

  32. quinten de wilde February 2, 2017 at 2:51 pm #

    Ok my laptop build failed because I did wrong cmake settings in the begin. My Imac is succeeding so far.
    Do I really need to do a fresh new installation (of osx) or is there another way to find all files in the installation en delete them to do a new installation (Opencv).

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

      I would simply delete your “build” directory, re-create it, and then re-run CMake.

  33. quinten de wilde February 2, 2017 at 2:59 pm #

    Amazing post !! Got it to work!

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

      Congrats, great job!

  34. Ye Tao February 3, 2017 at 9:44 am #

    Hi Adrian,

    Thank you so much for this tutorial. I’m a new programmer in MAC and opencv. I’m encountering a really silly problem.

    I’m stuck at step#7. The path of Python2 libraries cannot be defined. The directory was correct because when I type “ls + directory”, it shows the correct one (I also searched them stage by stage and it was correct). Then I change the python2_libraries using cmake, it shows “Cmake Error: The source directory does not exist.”

    I also added ” ..” by the end of the command, it still doesn’t work.

    Thank you in advance.

    • Ye Tao February 3, 2017 at 10:32 am #

      Hi Adrian, opencv has been installed. Thank you so much for the great help!

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

        Congrats on getting OpenCV installed!

  35. Aravind February 7, 2017 at 2:58 am #

    Hi adrian, may i know what is the difference between the installation process you documented here and this simpler version. Thanks so much for all the articles, love them!

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

      The gist is that the simpler version with Homebrew doesn’t require you to manually edit your CMake command. Homebrew attempts to create the CMake command itself based on your Python versions, then compiles and installs OpenCV for you.

  36. Pulkit Kumar February 19, 2017 at 11:10 pm #

    Hey Adrian,

    Firstly thanks for an excellent tutorial. This is clearly the best available on the internet.

    I am facing a problem at step 9 as my cv2.so file has not been made. I went up to see my cmake output and i saw that under Python2 only my interpreter was listed and the rest three outputs are missing. Could you please help me out as to where am i going wrong?

    Thanks in advance

    • Adrian Rosebrock February 20, 2017 at 7:40 am #

      Please see my reply to “Jonathan” above. Make sure you are in the “cv” virtual environment and double-check your paths to your Python libraries via the “ls” command.

  37. Shashank February 25, 2017 at 9:36 am #

    Adrian, I have a issue. I followed these steps and they worked perfectly. But now when I open my python shell and try to import cv2 or numpy or any of these libraries which are installed in my virtual environment, they give me an error. I understand that’s how its supposed to be but can you help me as to how should I access my virtual env through my python shell ? I can’t run the workon cv command as I do when I’m on my terminal.

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

      Can you clarify what you mean by your “python shell”? You should run “workon cv” before you access your Python shell:

      From there you’ll be able to import OpenCV, NumPy, etc.

  38. Thibault March 2, 2017 at 3:51 am #

    Hi, thanks again for this excellent and brilliant tutorial. I am stuck at the step 9 with cv2.so not existing while it seems everyhting runs perfectly.
    I noticed in the cmake that I do not see numpy library displayed while it is clearly installed in “cv” version and user python version: This is what I get.
    — Python 2:
    — Interpreter: /Users/Mac/.virtualenvs/cv/bin/python2.7 (ver 2.7.13)

    Do you think that may cause the issue?

    Thanks again, Thibault

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

      I would double-check that NumPy is indeed installed into you cv virtual environment:

      You should see NumPy in the list of output packages installed.

  39. Sumaiya Asif March 8, 2017 at 1:09 pm #

    Very specific and very helpful! Thank you for taking the time to write this up and helping others who are having trouble 🙂

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

      Thanks Sumaiya, I’m happy you enjoyed the post! 🙂

  40. Marc Streeter March 21, 2017 at 12:38 pm #

    Open CV 3.2 has been released, using the above instructions still work, after cloning both repo’s all you have to do is enter each directory and do git checkout 3.2.0 and follow instructions like normal

  41. Jose Antonio March 24, 2017 at 6:39 pm #

    Works perfectly fine with OpenCV 3.2.0-dev and Python 2.7.13

    Thank you very much

    • Adrian Rosebrock March 25, 2017 at 9:17 am #

      Congrats on getting OpenCV installed Jose!

  42. Michelle Morales March 29, 2017 at 7:33 am #

    Hey Adrian, Thank you so much for this awesome and informative post! I am also having trouble on Step #8 when I run: make -j4

    I receive the following error:

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

    Any insight into what the issue might be?

    • Michelle Morales March 29, 2017 at 9:51 am #

      Looks like I had a typo in my path to python. I was able to download it successfully.

      Thanks again for the tutorial, it was super helpful!

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

        Congrats on resolving the issue Michelle!

    • David Sixela September 22, 2017 at 10:06 am #

      Can you please explain how you solved this issue? i’m getting the same error

  43. Allie March 31, 2017 at 8:58 am #

    I was wondering if you could help me with the installation of OpenCV on my Mac (Sierra). I have been struggling for two days and am very close! But I keep getting this error at step 7. Maybe you would be able to lead me in the right direction.

    ‘CMake Error at 3rdparty/ippicv/downloader.cmake’

    • Allie March 31, 2017 at 9:07 am #

      More specifically, ‘Unknown CMake command “ocv_download”.’

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

        I would suggest double-checking your internet connection. CMake is trying to download an external file for the build. Make sure you have a strong internet connection when doing this.

  44. Srivani April 2, 2017 at 4:43 am #

    I am getting fatal error: ‘opencv2/highgui/highgui_c.h’ error after $ make -j4

    I have Seria OS,, any one had the same issues please help me

  45. haffj April 13, 2017 at 5:17 am #

    Hi,

    Great tutorial it helped me install opencv3.2.0 with python3.6.1.

    However I’m having very poor performances with the speed of processing of opencv on my computer (macOs Sierra 10.12.3) and I want to know if it’s due to my system or to the version of opencv I use. As an example it takes about 300 sec to cv2.detectMultiscale to execute on a single image, whereas the same script only needs a few milliseconds on another computer (MacOs El Capitan 10.11.6) with opencv3.1.0

    So I tried to download the 3.1.0 version and follow the tutorial but I had the QtKit error.
    Is it possible to fix it?

    • Adrian Rosebrock April 16, 2017 at 9:05 am #

      This tutorial will help you resolve the QTKit issue.

      As for the cv2.detectMultiScale issue, that is very strange. How large is your input image?

  46. Joe April 16, 2017 at 4:19 pm #

    I am having trouble with step 4. I have followed steps 1-3 meticulously. Would anyone happen to know what would return this error?:

    /Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python: No module named virtualenvwrapper
    virtualenvwrapper.sh: There was a problem running the initialization hooks.

    If Python could not import the module virtualenvwrapper.hook_loader,
    check that virtualenvwrapper has been installed for
    VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python and that PATH is
    set properly.

    I am running version 10.12.4 Sierra and cannot get this to work for the life of me! Any help would be greatly appreciated.

    • Lucas Alves April 25, 2017 at 6:47 pm #

      Could you solve this problem? I am having the same one.

      It would be good to know news from you.

      • Adrian Rosebrock April 25, 2017 at 9:02 pm #

        Try overwriting the Python sym-link in Homebrew:

        $ brew link --overwrite python

    • Bill Behenna June 3, 2017 at 7:55 pm #

      I have the same problem

  47. Richard April 27, 2017 at 4:48 pm #

    Thanks so much for the tutorial. Usually with these sort of installations there’s a whole bunch of problems even when given a step-by-step guide. But this one had zero problems for me.

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

      Fantastic, I’m glad to hear it Richard! Congrats on getting OpenCV installed.

  48. Yiliang April 28, 2017 at 10:45 pm #

    Hi Adrian,

    After the cmake step following your instructions, it shows that libraries = No. Where can it be wrong?

    Thank you

    • Adrian Rosebrock May 1, 2017 at 1:44 pm #

      Either your PYTHON2_LIBRARY or PYTHON2_INCLUDE_DIR paths are incorrect. Double-check these paths using the instructions I detailed in this tutorial.

  49. Alex May 5, 2017 at 1:33 am #

    Thanks a lot. It worked well until when I try to read and save video. Always back “can’t save” and “!!! BUG: The current event queue and the main event queue are not the same. Events will not be handled correctly. This is probably because _TSGetMainThread was called for the first time off the main thread.” Maybe the ffmpeg didn’t link with opencv. I have no idea to solve it. Hope you can help me. And I want to make sure whether from step5 is all in cv environment or not?

    • Adrian Rosebrock May 8, 2017 at 12:40 pm #

      Hi Alex — I’m not sure regarding your error message, I haven’t encountered that error before. And yes, for simplicity, make sure you are in the “cv” virtual environment for the tutorial.

  50. Jake Hadar May 11, 2017 at 9:12 am #

    Very helpful write-up Adrian, thanks for this!

    Anyone having cmake issues, make sure you cd into build/ before running

    Jake

    • Adrian Rosebrock May 15, 2017 at 9:05 am #

      Thanks Jake!

  51. Ayman May 14, 2017 at 7:03 am #

    This is an amazing tutorial ! Thanks Adrian

    On a different note, I need to reinstall opencv with the ffmpeg flag (i.e. openccv –ffmpeg) so that I can read AVI and MP4 videos properly, may I ask which step should I repeat to make that happen ?

    Thanks !

  52. Chris May 17, 2017 at 10:06 pm #

    Worked perfectly! Thanks for putting this together in such detail – excellent!

    • Adrian Rosebrock May 18, 2017 at 11:50 am #

      Congrats on getting OpenCV installed Chris, nice job!

  53. Erick Del Orbe May 30, 2017 at 10:16 pm #

    Although this looks like a great tutorial I achieved the same thing using the brew command:

    brew tap homebrew/science
    brew install opencv3 –with-contrib –with-cuda –with-ffmpeg –with-tbb –with-qt5

    At the end it gives you the command for python bindings which is:

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

    • Adrian Rosebrock May 31, 2017 at 1:05 pm #

      Hi Erick — I would also suggest that you refer to my OpenCV install with Homebrew tutorial. This tutorial is meant for readers who want to compile OpenCV from source.

  54. Edgar Acosta June 1, 2017 at 4:05 pm #

    Following this procedure I have a problem: When I run the $cmake. according to step 7. I replace what suggested, the paths: Python Library and Python Include Dir. So CMAKE finished with no Problems, and then I run $make j4. When finished I check the existence of cv2.so. and it is MISSING! is not in the directory. Checking the output of $cmake. I realized that in the section Python 2: in Libraries, it says NO. it supposed that the Path to libraries shall be there. But is missing. The other Parameters are OK

    Any idea of whats wrong?

    • Adrian Rosebrock June 4, 2017 at 6:22 am #

      If the “Libraries” section is “NO”, then double-check the path you supplied. Use the ls commands like I suggested in the post to help you determine these paths.

  55. Kyle June 2, 2017 at 3:38 pm #

    Hi Adrian,

    I’m still not sure how I to find the exact path to my libpython2.7.dylib file. When you say, “just use the ls command along with the wildcard asterisk” — what should I be typing in the terminal?

    $ ls * which would literally be the ls command along with the wildcard asterisk doesn’t work.

    Then I tried using: $ ls /usr/local/Cellar/python/2.7.* and that didn’t work either.

    I’m lost… thank you for any help!

    • Adrian Rosebrock June 4, 2017 at 5:39 am #

      Hi Kyle — to start, make sure you have installed Python via Homebrew. You can validate this using the instructions earlier in the blog post. Secondly, I’m not sure what you mean by the “command doesn’t work”. Are you getting an error of some sort? Is the path invalid? If the path is invalid, I think you need to double-check your Python install.

  56. Alfredo June 5, 2017 at 8:48 pm #

    Hi! A comment, use:

    brew link python

    not “linkapps” because it will be deprecated.. I put link and it worked 🙂
    Thank’s for the tutorial!

    • Adrian Rosebrock June 6, 2017 at 11:58 am #

      Thanks for sharing Alfredo!

  57. margas June 12, 2017 at 9:47 am #

    Hi Adrian! I would like to thank you for this great tutorial! I have managed to install opencv and python successfully on my Mac. But I face a problem. When I try to develop some code that has as an output a pop up window it seems that everything is working properly except of the pop up window. I can see the tree dots that represent minimise, closure etc but no the rest of the pop up window. Do you have any idea why?

    • Adrian Rosebrock June 13, 2017 at 10:59 am #

      Just to clarify, did you use this tutorial when compiling and installing OpenCV? It sounds like there is an issue with the GUI backend compiled with OpenCV.

      • margas June 13, 2017 at 12:57 pm #

        So what do i have to do? Yes i followed this tutorial.

        • Adrian Rosebrock June 16, 2017 at 11:38 am #

          Without physical access to your machine, I’m not sure what the exact error is. Can you try installing XQuartz, then re-compiling and re-installing OpenCV?

  58. Sai Sudheer June 13, 2017 at 10:09 am #

    Hi all,

    I NEED SOME HELP !!

    From the step 7, I accidentally copied the CMAKE Template as it is and ran it. Now
    In Python 2, the path in Libraries is showing YYY. Please help me to get rid of this.

    • Adrian Rosebrock June 13, 2017 at 10:50 am #

      Simply delete your build directory, re-recreate it, and re-run CMake.

  59. Carlos Villegas June 16, 2017 at 7:35 am #

    Hi Adrian.
    Great tutorial, but I have been stucked in step #7 for several hours.
    After execute the cmake template with the libraries and directories ok then appear the follow message:

    Configuring incomplete, errors occurred!
    See also “/Users/villegascarlos/opencv/build/CMakeFiles/CMakeOutput.log”.
    See also “/Users/villegascarlos/opencv/build/CMakeFiles/CMakeError.log”.

    Any suggestions??

    • Adrian Rosebrock June 16, 2017 at 11:09 am #

      Hi Carlos — you are receiving that error message because there was a problem with CMake. Scroll up through the CMake output to find the error.

    • Nice.Y August 10, 2017 at 3:42 am #

      I have the same problem with you. Have you solved it? 🙂

  60. Sid June 19, 2017 at 11:20 am #

    Struggled a bit with setting up on Sierra but finally worked.
    Thanks Adrian!

    • Adrian Rosebrock June 20, 2017 at 10:56 am #

      Congrats on getting OpenCV installed, Sid — nice job!

  61. David Foster June 24, 2017 at 4:36 pm #

    Hi Adrian,

    Thanks for your awesome series of blog posts on openCV – they’re super helpful.

    However, I’m in a bit of a dilemma. If I install the bleeding edge 3.2.0 version of openCV on mac Sierra (python 2.7) I run into this error when using waitKey

    https://github.com/opencv/opencv/issues/5874

    The workaround is to revert to 3.0.0 – but then I run into the QTKit error, for which the solution is to use 3.2.0!

    What should I do? If you can provide any guidance it would be much appreciated.

    Thanks,

    • Adrian Rosebrock June 27, 2017 at 6:35 am #

      I personally haven’t ran into this issue before, so I’m not sure regarding the best course of action. You mentioned installing the bleeding edge version of 3.2.0. Does the same issue happen if you use git checkout to checkout the tagged 3.2 release?

    • Eric So June 28, 2017 at 4:30 pm #

      Hello David,

      I am having a similar problem with videowriting. I tried cv 3.2.0 (python 2.7.13)both with and without virtualenv and they have problems of their own. Did you find a solution for this?
      Thanks!

  62. Murthy July 12, 2017 at 3:11 pm #

    Hi Adrian,
    Thanks for the blog on installing CV2 on Mac. This worked for me…my previous attempts failed miserably and I started fresh by reinstalling Mac OS and then following the instructions in this blog.

    • Adrian Rosebrock July 14, 2017 at 7:34 am #

      Congrats on getting OpenCV installed Murthy, great job! 🙂

  63. Satvik July 17, 2017 at 2:29 pm #

    Thanks a lot! It took a lot of time. Dont miss any step and make sure every step is correct.

    • Adrian Rosebrock July 18, 2017 at 9:56 am #

      Congrats on getting OpenCV installed Satvik, nice job!

  64. Phillip July 17, 2017 at 4:36 pm #

    Hi Adrian,

    Thank you for this tutorial it is very helpful!

    I just have a quick question regarding the final step. When I type in the command: “cv2.__version__” it returns ‘3.3.0-rc’. Is this an issue?

    Thank you,
    -Phillip

    • Adrian Rosebrock July 18, 2017 at 9:52 am #

      That simply means that you are running the 3.3 “Release Candidate”. It’s not a “true release”, just a “candidate” for a release. It potentially could have a few bugs, but likely no more than previous versions.

      • Phillip July 18, 2017 at 11:09 am #

        Okay great, thank you for the quick response!

      • Fatma July 25, 2017 at 5:25 pm #

        Hi Adrian

        I also got the ‘3.3.0-rc’ version instead of ‘3.1.0-dev’ how can I get the ‘3.1.0-dev’ version instead ?

        Thank you very much

        • Adrian Rosebrock July 28, 2017 at 10:05 am #

          After you clone down the Git repos for both “opencv” and “opencv_contrib”, you’ll want to checkout the “3.1” branch (I suggest using tagged versions instead of the dev versions):

          Do the same for “opencv_contrib” as well.

  65. Nice.Y August 9, 2017 at 9:44 pm #

    I have followed the first 7 steps, when I did $ make -j4, I got the error”fatal error: ‘QTKit/QTKit.h’ file not found”.So how can I solve this?
    Thanks very much 🙂

    • Adrian Rosebrock August 10, 2017 at 8:44 am #

      Please take a look at the “Avoiding the QTKit/QTKit.h file not found error” section of this post. It sounds like you might not be running macOS Sierra (or greater).

      • Nice.Y August 13, 2017 at 1:46 am #

        Hi, Adrian
        I’m sure that I’m running macOS Sierra. And I noticed that in this post, you said “the trick is that we need to use the HEAD of the repo as opposed to a tagged release”. I’m not quite understand what it means.
        Thank you very much 🙂

        • Adrian Rosebrock August 14, 2017 at 1:11 pm #

          I would suggest reading up on git and the difference between tagged releases and the HEAD of repositories. This StackOverflow thread does a nice job.

  66. Chris Z August 18, 2017 at 11:50 pm #

    After I installed virtualenv virtualenvwrapper and changed bash_profile, I run source ~/.bash_profile and I got this:
    -bash: /usr/local/bin/virtualenvwrapper.sh: No such file or directory

    • Adrian Rosebrock August 21, 2017 at 3:46 pm #

      This might be an issue with your Python + Homebrew install. Take a look at this solution.

      • Chris Z August 23, 2017 at 2:03 am #

        Hi Adrian,

        I guess this solution does not solve the problem that I am still using python from the system. I think it may be because of the update of brew. Now brew probably installs python in a different path

  67. Eason Wang September 22, 2017 at 9:10 am #

    Good work. But when I try to follow these step to install OpenCV3.3 with Python 2.7. I run into some problems. The first one is by running “which python” I cannot “/usr/local/bin/python” anyway. Did you validate this method for OpenCV 3.3? Thank you!

    • Adrian Rosebrock September 23, 2017 at 10:10 am #

      This method will work with OpenCV 3.3. However, to updates to how Homebrew handles Python make it a bit more complicated. Because of this, I’ll be releasing an updated OpenCV 3.3 + macOS install tutorial on Friday, September 29th 2017. Please check the blog post then and you’ll find updated instructions.

  68. Daniel September 28, 2017 at 5:26 am #

    In Step #3: Setup Homebrew for Python 2.7 and macOS.
    I already followed the description above.
    However, the output is still /usr/bin/python. I tried so many times, do you have any ideas? thank you 🙏🙏

    • Adrian Rosebrock September 28, 2017 at 8:56 am #

      What is the output of:

      Are those two correct?

      • Daniel September 28, 2017 at 10:33 pm #

        Yep! It shows /usr/local/bin/python2
        Does it mean that I need to add 2 after python in the following steps?
        Thank you!

        • Adrian Rosebrock October 2, 2017 at 10:41 am #

          Thank you for the clarification.

          I would suggest explicitly setting your Python version inside your ~/.bash_profile, like this:

          From there, supply the -p switch to mkvirtualenv when you create your Python virtual environment:

          $ mkvirtualenv -p python2

  69. Daniel September 29, 2017 at 1:13 am #

    My terminal showed -bash: pip: command not found when I type:
    pip install virtualenv virtualenvwrapper…

    • Adrian Rosebrock October 2, 2017 at 10:22 am #

      Pip should be installed by default here. Can you please confirm which version of Python you are using?

Trackbacks/Pingbacks

  1. macOS: Install OpenCV 3 and Python 3.5 - PyImageSearch - December 5, 2016

    […] week I covered how to install OpenCV 3 with Python 2.7 bindings on macOS Sierra and […]

Leave a Reply