How to Build OpenCV for Python in Windows

Online documentation for OpenCV is spotty at best. And for many users, OpenCV’s full set of features doesn’t work in pre-compiled builds. This can be a serious barrier to entry for developers and students who want to write computer vision applications. This guide is designed to make the installation process less painful for Windows developers.

This also includes steps for building OpenCV contrib (aka OpenCV_NonFree), which contain APIs for the popular (but copyrighted) SIFT and SURF algorithms.

If you are interested in building OpenCV for C++ or Android, just skip steps 5 and 7.

My Hardware and Version Info

Operating System: Windows 10 Enterprise
Processor: Intel i7-6700K
Python Manager: Anaconda3 4.2.0 x64
Python Version: 3.5
OpenCV Version: 2.4.13
Cmake 3.6.1 x64
Git 2.9.3 x64
Visual Studios Community 2015

Required Software Downloads

  1. Anaconda for Python 3.5
  2. Cmake x64 Installer
  3. 64-bit Git for Windows Setup
  4. Visual Studios Community
A note on OpenCV version:

OpenCV is available in 3.x and 2.4.x flavors. This reflects different development branches. For the purposes of this guide, we will focus on OpenCV 3.x.

1. Install Git

Download Git for Windows. In this guide, I will be using Git Setup (non-portable) 2.9.3 64-bit version of Git for Windows.

Run Git-2.9.3-64-bit.exe and follow the installation wizard with the following options selected.

git_install_2 git_install_3 git_install_4

2. Install Visual Studios Community

You’re just installing Visual Studios for the C++ compiler and Python Tools. The other options depend on your preferences, but don’t matter much for this guide.vsc_install_1

3. Install Cmake

Install cmake-3.6.1-win64-x64.msi. Select whichever options you wish. For this guide, I installed Cmake for all users on my computer.

4. Pull the latest OpenCV Build from Github

Open Git BASH. Go to whatever directory you want to store your OpenCV files. For me, I navigated to my Development directory.

In Git Bash enter the following command:

This will create a copy of the latest OpenCV source on your local computer.

4.1 OpenCV_contrib

Then pull the latest OpenCV_contrib source. For the purposes of this guide, I recommend NOT putting your OpenCV_contrib build directory into a subdirectory of your OpenCV build directory.

5. Install Python with Anaconda

Of course you don’t need to use Anaconda. But it’s my favorite Python manager, especially in the Windows development environment. So it’s what I’ll be using in this guide.

Go to and download the Windows 64-bit installer for Python 3.5+. As of the time this guide was written, the current version was Anaconda 4.2.0.

Although the default installation is for 1 user, I think it’s easier and more useful to install Anaconda and Python system-wide. This will mean that Anaconda’s install paths will be clean and neat, like:

As you go through the installation wizard, make sure you check these 2 boxes. This makes Anaconda’s version of Python your system’s default version of Python. It also adds Anaconda to your system PATH, which makes using command line tools easier.

6. Building OpenCV

OK, here is where things start to come together. First, we are going to select our OpenCV build options with CMAKE. Then we are going to feed those CMAKE-generated build option to our Visual Studios compiler.

Aside: If you plan to do any GPU-enabled computations with OpenCV, such as Deep Learning, I highly recommend downloading and installing CUDA, CuDNN, and the related drives from Nvidia before building OpenCV.

In Git Bash, navigate to your OpenCV folder and enter the following command:

This ensures you are using the most up-to-date version of OpenCV.

Next, open up the CMAKE GUI.cmake_base
Now select Browse Source and select the folder that contains CMakeLists.txt. For me, this is just my base ../opencv/ directory. Then create a new nested directory ../opencv/builds. Now select Browse Build and select that folder you just created.

Next, hit Configure only once. You should be prompted with which compiler to use. We want to use the x64 version of the Visual Studios compiler we just installed. In our case, that’s Visual Studio 12 2013 Win64.

Then, you should see something like this:

As an aside, these build options are easier to navigate when the Grouped flag is checked.

Now we perform a sanity check. Open up the PYTHON3 group. The paths should be automatically populated, and it should look like this:
If not, then you have to manually enter each of these values. This is very important. It’s OK if your PYTHON2 paths are blank.

NOTE: If the OpenCV source you pull from Github does not already include OpenCV_contrib, you must follow the steps outlined in Section 4.1. You must then copy your <opencv_contrib_build_directory> path into the OPENCV_EXTRA_MODULES_PATH in CMake. As astute user Shrinivas Kudekar notes, the OPEN_CV_EXTRA_MODULES_PATH path should be
~/opencv_contrib/modules rather than just ~/opencv_contrib.

Aside from the defaults, I am checking:

Then, click Generate.

Now you should have a file called OpenCV.sln in ../opencv/builds. Go ahead, and open that file up with Visual Studios.

First, select ALL_BUILD. If you value your time, change your compiler from Debug to Release. Then Build.
This should take a while. Go get a coffee.

Then build INSTALL. This should also take a while. Go get another coffee.

Note: Some users have reported that ONLY build INSTALL is needed, and doing build ALL_BUILD beforehand is unnecessary. Please let me know what works for you.

If anything goes wrong, and you need to restart the build process:
1. Delete everything in your ../opencv/builds directory
2. Go to File -> Delete Cache in CMAKE
3. Start the build process over again.

Finally, you should go to Environment Variables in Control Panel and add to the path variable:

7. Testing your OpenCV Build in Python

Through Anaconda, open up an instance of Spyder. Create a file called Copy and paste the following lines:

If you are able to import OpenCV and detect the SIFT-keyword functions, you should be good to go.

Good luck!

If you have found any errors, please leave a comment. I will do my best to respond as quickly as possible!

17 thoughts on “How to Build OpenCV for Python in Windows”

  1. Hi, I meet a mistake when I use VS to build the ALL BUILD item, which is “56 successful 1 failed”(cannot find “opencv_version.exe in Release folder”)
    Before that I have made the ALL BUILD builded successfully but when I build INSTALL my “red umbrella” warn me and delete one of my files, so I go over all these again and I shut down it.
    From that on, I cannot make all 57 successfuly. Could you tell me what should I do?

  2. I cannot import cv2 could you show your PYTHON3 LIBRARY path more clearly I have to manully add this path but I am not sure which one I should add.

  3. Hey Eric

    Thanks a lot for this site. With this I could build opencv for the first time (after some failed attempts with others). the only thing is: The python test script does not work in my spyder. It says after find_function() definition there should be identation. After I inserted identation it says it could not import cv2 (line 3). Do I need a path definition to cv2 in the opencv folder?

    Thank you for help!

    1. Hi Frank and TT.
      Thanks for the nice comments.
      Yes, you need to let conda (Anaconda’s package manager) know where cv2 is.
      The easiest thing is to add cv2 to your conda search path.
      You can also bundle cv2 or any other package into a conda package.
      I don’t have my machine in front of me, but for more details see:
      Let me know if this works for you.

      1. Hi,

        Thanks a lot for the great tips to install opencv with sift and surf enabling. I followed all the instructions, but still import cv2 gives me the error “DLL load failed: The specified module could not be found.”

        I checked on the web and I have python3.dll and pyhton36.dll inmy Anaconda3 directory. I also have the file “cv2.cp36-win_amd64.pyd” in the directory: Anaconda3\Lib\site-packages.

        I tried changing the name of the .pyd file to cv2.pyd. That also doesn’t work. I am using Windows 10 and Spyder in my Anaconda environment.

        Please any suggestions and tips would be greatly appreciated.

    2. Small corrections to otherwise perfect recipe for installing opencv with access to SIFT and SURF. Thanks a lot to the author of this website for carefully detailing all the steps. Minor suggestions which worked for me.

      (0) We need to download both opencv and opencv-contrib
      (1) When you set the
      OPEN_CV_EXTRA_MODULES_PATH, the path should be

      and not just ~/opencv_contrib

      Otherwise, the CMAKE will give error when you press “Generate”

      (2) Finally, you should go to environment variables in Control panel and add to the path variable:


      This will remove the errors “DLL: Load failed” when you use import cv2 in Spyder.

      Took a while, but got it working finally!

  4. Thanks for the tutorial! This helped more than any of the slew of other sites that I visited.

    Why didn’t you have to pull the opencv contrib file off github and put its path in the OPENCV_EXTRA_MODULES_PATH on CMake?

    Also when it came to building in Visual Studio, all I had to do was build INSTALL and then ALL_BUILD was already built when I tried to build it.¯\_(ツ)_/¯

    1. Thanks Victor!
      Yep, looks like I forgot to include the steps for OPENCV_EXTRA_MODULES. Thanks for catching that, I’ll make the correction.
      And I’m not sure if your versions are the same as mine, but INSTALL should also work. Good catch again.

    2. On second inspection, you might not need to pull the opencv contrib file off github and put its path in the OPENCV_EXTRA_MODULES_PATH on CMake if instead you set the CMAKE flag OPENCV -> OPENCV_ENABLE_NONFREE
      I assume this would only work if the version of OpenCV you pull off github already includes the OpenCV_contrib source, but is not build by default.
      Feel free to let me know which methods work for you.
      Thanks again!

  5. hi
    How do you run this?

    sift = cv2.xfeatures2d_SIFT()
    this works after habing build


    kp1, des1 = sift.detectAndCompute(img1,None)
    this gives an error

    descriptor ‘compute’ requires a ‘cv2.Feature2D’ object but received a ‘numpy.ndarray’me

  6. It’s really a great and helpful piece of info. I’m happy that you
    just shared this useful info with us. Please stay us up to date like this.

    Thanks for sharing.

  7. Thank you for providing these instructions, Eric. I greatly appreciate it.

    In your instruction step # 4, when I open a git shell and type

    cd <H:\Packages\Programming\OpenCV&gt

    where H:\Packages\Programming\OpenCV is to be my build directory I get the following errors:

    [1] 21812
    bash: lt: command not found
    [1]+ Done cd
    [1] 16940
    bash: H:PackagesProgrammingOpenCV: command not found
    bash: gt: command not found
    [1]+ Exit 127 H:\Packages\Programming\OpenCV

    Do you have any ideas about why this would be happening?

    1. symbol like &gt represents the > sign. It isn’t meant to be entered. It is telling you to substitute your file name for the pseudo file name the author used.

  8. hi.thanks for your great recommendations.
    I have followed all the above steps.but still getting this error:
    *** module ‘cv2.cv2’ has no attribute ‘xfeatures2d’***
    I am using :
    *python 3.7.1 through anaconda
    *opencv 4.1.0
    (I install the same version through .whl files
    but couldn’t find and download opencv 4.1.0 folder to use as source in cmake configuration)
    the camke steps has done completely .
    could any one help me please?

  9. I had a problem with the CMake CGI. After a lot of output that was successful, a message appeared “IPPICV: Download: CMake Error at cmake/OpenCVDownload.cmake:231 (Message): IPPICV: Unpack failed: Acces is denied. ”
    I was able to proceed when I disabled PCMatic Supershield and ran the CMake CGI over again. I hope this helps other users.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.