Setup OpenCV for Python in Windows

I have always struggled when trying to set up OpenCV for Python before. So I decide to write this post to help myself in the future and share with you =). My setup is for OpenCV 2.2 but I think you can apply for any version of OpenCV.

Step 1: Download and install Python 2.7 from You need to install the 32bit version of Python. OpenCV currently does not work with Python 64bit.

Step 2: Download and install OpenCV 2.2 from Note that this version only supports Python 2.7 (not 3.x).

Step 3: Download and install NumPy 1.6.1 and SciPy 0.9.0 from: (you need to choose the files which support Python 2.7)

Step 4:Setup Windows Path in Environment Variables

  • Add “C:/Python2.7;C:/OpenCV2.2/bin” to PATH variable (You need to change the directory to where you install Python and OpenCV).
  • Create PYTHONPATH variable and put “C:/OpenCV2.2/Python2.7/Lib/site-packages” as value.

Update: For OpenCV2.3, There are two other options for this step:

  • As Kyle commented below, you can copy the content of folder “C:opencvbuildpython2.7” (for opencv 2.2: C:/OpenCV2.2/Python2.7/Lib/site-packages) to folder “C:Python27Libsite-packages
  • OR, add these two lines at the beginning of your program:

Yosh!!! You have finished setup OpenCV for Python in Windows. Open Python IDLE, create a new file, add the program below and run it:

(This is the program to show your webcam)


  1. OpenCV has just released version 2.3.1 which is more stable than 2.2 I think. You can download it here and try it (the setup is same as 2.2).
  2. About the black screen you encounter with the example, I think it’s because your camera is not supported. My camera works fine. You can find more information here.
  • Phong

    Good job. It took me a lot of time as well. I have done what you did, but at some point it turns out that some functions are not available although described in the documentation. I had to compile OpenCV with C++, and then link Python to OpenCV.

    But seems like OpenCV 2.2 has better support for Python ^^.

    • Gia Thuy

      Thanks Phong :-)

  • Notia

    Thank you very much for your explanations. They help me a lot.

    • Gia Thuy

      You’re welcome :-)

  • Leo

    Hello!…. I’m fairly new and I have struggled quite a lot trying to make OpenCV works with Python (specifically in Eclipse)…. in step 4 where do i set the windows path (in the ‘MyComputer’_ properties_ advance_environment variables? if yes, do I create PYTHONPATH in the “user variables” or in the “system variables”?
    Thank you


    • Gia Thuy

      Hi Leo,
      You can create PYTHONPATH in the “system variables” so that other users can use it. You can refer to this for different between “user variables” and “system variables”. Hope it helps.

      • Leo

        Cool..thanks!!…. one thing, have you gotten any issue with CaptureFromFile?… whenever i run that command my program crashes…


        • Gia Thuy

          Hi Leo, I have also encountered this problem. I have tried to search the solution, but could not find it (The developers of OpenCV have fixed this problem, but I think they have not released the patch yet. You can find it here
          If you want to use this function, I think you should use version 2.1 instead.

        • Gia Thuy

          Hi Leo,
          I have just built the source code of OpenCV 2.2 (downloaded from their SVN) in Windows. It worked perfectly for CaptureFromFile :-D To build the source, you can follow the instruction here. Hope it helps.

  • Rehan

    Hey gia

    When I run the code, the output is all black on the window. When I tried it on another computer, it asked me in a dialog box, displaying camera selected is an integrated cam. Then the screen is just gray.

    I added the variables as you said. The scipy and numpy files you have provided links for are not compatible with python 2.7, I had to download the compatible versions.

    Another problem which I faced was that even though I had added the environment variables, I had to change the “inlcude” variable path aswell as the “Lib” paths to opencv and python2.7. You didn’t mention that. Other wise I was facing an error with “import cv” command regarding numpy and multiarray crap.

    I have run some sample programs in opencv2.2. Some of them run while others give error with “urllib2″ or ”
    “sys”. Please help me run the camera

    • Gia Thuy

      Hi Rehan,

      For the scipy and numpy install files, I just provided the links of the folder to download the correct version. You should not download the recommended download file.
      What do you mean “change the include variable and lib paths to opencv and python2.7″. I think we do not need this for opencv to run in python :-? After you install python 2.7, then correct version of numpy and scipy (which are supported python 2.7), they will be add to the folder Libsite-packages of Python. Would you like to check whether they are there or not? If not, I think you need to install numpy and scipy again.
      About the problems regarding to “urllib2″ or “sys”, can you give me the code of the program?

  • Nick

    You are my hero! I just wasted hours trying to compile openCV with MinGW and Cmake, but it kept failing when it encountered python objects… now I think the problem may have been that I didn’t have scipy installed. Might have to try that…

    • Gia Thuy

      Thanks for your comment =) Hope that it will solve your problem. Let me know if you encounter any issue =)

  • Elly

    Hi There,
    Thanks alot for this wonderful tutorial, it really helped me get where to start in terms of how to install Opencv but well am having problems still. I can’t be able to use the library because i get an error in my IDE(Eclipse).
    I followed the steps you listed.
    Am using Windows 7 as my Operating system , OpenCV2.2 , Python2.7 plus the rest of the libraries that you talked about for python2.7.
    However i get the following error in my IDE when i try to import the cv library.
    “Unresolved import:cv”
    Have tried to look for a solution for two days now but things have not worked out.
    Any help will be greatly appreciated

    • Gia Thuy

      Hi Elly,
      I have not tried Eclipse to code python opencv. Can you add these lines below at the beginning of your code to see whether it work:

      import sys

      You need to change the opencv path to the directory you installed it.

      Hope it will work for you :-)

      • changtraidoc

        it still don’t work :(( :((

        • Gia Thuy

          What’s the problem you encountered?

        • Gia Thuy

          OpenCV’s just released version 2.3 which is more stable than 2.2 I think. I’ve just tried it and it worked fine. May you try it? :-)

  • Kyle

    Thank you very much!

    One comment:
    I actually had to copy the contents of “C:OpenCV2.2Python2.7Libsite-packages” into “C:Python27Libsite-packages”, otherwise python couldn’t find the cv package and I got “ImportError: No module named cv”

    • Gia Thuy

      Hi Kyle,
      Thank you for your comment. If you set the PYTHONPATH in your environment variable as I describe. You don’t need to copy that folder.

  • Ryan

    Good instructions, thanks. I am able to get your code to run, unfortunately the ‘camera’ window is solid black. I can see the LED on my webcam turn on, so it is accessing the camera.

    Do you have any suggestions on how to debug this? Do I need to install support for my webcam (Microsoft Lifecam) or configure opencv to read from it properly?

    I found this thread on StackOverflow, but no luck using their code yet either:


    • Gia Thuy

      Hi Ryan,
      I have not encountered this problem. Was there a dialog window for selecting the camera? Can you try to pass other values to CaptureFromCAM, such as -1 or 1 instead of 0.

  • Vasco

    Hi Thuy,

    Have you have run into problems in accessing AVI or MPG files in OpenCV2.2 and Python2.7? Of the 4 computers in which I have installed OpenCV using your detailed instructions, in only one of them the acquisition is functioning… I use that computer to program, and then I create a .exe (using py2exe) that runs well in all of them. But if I try to run for example the simple script in the other 3 PCs:
    import cv
    videofile = cv.CaptureFromFile(‘a.avi’)
    frame = cv.QueryFrame(videofile)
    print type(videofile)
    print type(frame)

    it returns:
    type ‘cv.Capture’
    type ‘NoneType’

    OpenCV seems to be correctly installed in all computers and functions perfectly well for static images (jpgs) and I can run all the examples of the OpenCV Python install. It’s only the videos that are not read… I have installed and uninstalled several codec packages without success (but when I uninstall K-lite for example, not even my .exe functions). So, I suspect it is really a codec problem… Any idea on how to solve this and get it to work in all computers? Thanks.


    U developed a nice application i tryed it out its working fine.
    I am using the same bluetooth chat application to develop Bluetooth tic tac toe game .Playing game via bluetooth between two android phones
    Can i get ur help .

  • jojaquix

    i have the same problem a black frame, think because i’m working with windows 7.
    the rest of examples work fine.
    with opencv 2.1 i think works, but i need 2.2

    • Gia Thuy

      OpenCV has just released version 2.3. I’ve just tried it and it worked fine on my laptop. I think they have fixed the camera’s problem. May you try it?

      • jojaquix

        Yes, i tried with it, but the problem now is the suport for cv import, cv2 work fine but not cv.


        I will try with this version:

        • jojaquix

          Yes works fine, the two imports (cv and cv2). In addition the web page is very useful.

          • Gia Thuy

            Thanks jojaquix :)

        • sachin


          I cant seem to import cv. Keep getting the error: (import cv2 works fine, but I cant run your example code with cv2)
          ImportError: DLL load failed: %1 is not a valid Win32 application.

          Do you have any idea how to fix this. Appreciate any help.


          • jojaquix

            Hi, try with
            works fine for my on Win 7 x86.

          • Gia Thuy

            Hi Sachin,
            I think it’s because you are using Python 64bit. You should use 32bit version for opencv. Hope it helps!

  • amogha

    I get a error saying “ImportError: DLL load failed: %1 is not a valid Win32 application.” when I import cv module, please help me out

    • Gia Thuy

      Hi Amogha,
      I suspect that you are using python 64bit. I think you need 32bit version to use opencv. Hope it helps!

      • John V. Busch

        I successfully compiled OpenCV 2.4.1 64bit with VS2010 Express on Win7-64.

        I am now stuck on the same error as Amogha.

        I really need to use 64bit python for other areas of my application, mainly in that it will be quite a memory intensive application and being limited in RAM(opencv will mainly be used for video) is a very bad idea for my client’s needs.

        Are you aware of any bug fixes in the works (also tried to register at to report my own and am having trouble with their registration process)?

        Any insight would be greatly appreciated.

        John V. Busch

        • John V. Busch

          For the record, I had OpenCV 2.1.0 64bit working in Python 64 on Windows 7-64 prior to my latest effort to upgrade to 2.4.1; albeit there seemed to be bugs.

          John V. Busch

  • Nolwenn

    What a shame… only 32bits versions,
    it could be reallllllly useful to have a 64 bits ver
    especially when you deal with Python…

  • kavish

    My python.exe frezeze when i try
    import cv
    im= cv.LoadImageM(“airplane.jpg”)

    also is there any tutorial when i can learn the opencv for python and .

    • Gia Thuy

      Hi, which version of opencv are u using? I prefer 2.1 or 2.3 since the 2.2 one is not stable I think.
      You can looking for the book “Learning OpenCV: Computer Vision with the OpenCV Library”. The code of the book is in C++, but you can easily convert to Python. Hope it helps!

  • eko_rudiawan

    hy,, i have get new problem. i have success installed but when i try your code i didn’t see picture on frame but my webcam is on,, why ?? can u help me

  • chenwei


  • mike shaulov

    I’ve tried to install OpenCV 2.2, OpenCV2.3 and 2.3.1 on Windows7 64bit with python 2.7.1 32bit.
    OpenCV 2.2 loads but shows black screen when running the sample.

    When trying ‘import cv’ with OpenCV 2.3 and 2.3.1 I get:

    RuntimeError: module compiled against API version 6 but this version of numpy is 4
    Traceback (most recent call last):
    File "", line 1, in
    ImportError: numpy.core.multiarray failed to import

    Does anyone knows how to solve the problem?


    • Gia Thuy

      Hi mike,
      Can you try to install the latest version of numpy 1.6.1 for python 2.7 at
      I have just tried it and it works find for OpenCV2.3.1

    • Gia Thuy

      And for the black screen when you run the example. I think it’s because your camera is not supported =( I can’t help with this.

  • Jason

    Hi Thuy,

    I installed both Python and OpenCV and get the camera capturing program work.

    But still I cannot load an image from a jpg file. I tried it in IDLE and it replies with:


    And I’ll have to import cv again.

    Have you ever encountered such situation? Thank you.

    • Jason

      Alright now. Just some problem in compiling.

  • Chris

    I believe I followed the guide exactly. I installed python 2.7.2, I installed numpy and scipy versions specified, built opencv according to this guide: , added the environment variables, restarted the computer, and I still am getting this error: “ImportError: DLL load failed: The specified module could not be found.” on the line “from import *” in Any help would be very appreciated, as I have already wasted way too many hours trying to get this to work.

    • Gia Thuy

      Hi Chris! Did you install the 64-bit python? You need to install the 32-bit version. I think that’s why you got DLL error.

      • Chris

        No, I have 32 bit python. Thanks for the replay!

        • Chris

          erm, *reply

      • Chris

        Any other ideas?

        • Gia Thuy

          Hi Chris. Can you try open Python IDLE and execute “from import *”?

          • Chris

            Same error. :(

          • Chris

            Was the guide I mentioned in OP sufficient for python support? Everybody says “Install open cv”, which turns out to actually mean (at least as far as I have found) to compile and install it (which I think I have done successfully). Are there any binary installers for opencv, or is the only way to get it by source? I don’t completely trust that it compiled successfully, even though it didn’t give compile errors.

            Also, if it is important, I am running windows 7 32 bit.

          • Gia Thuy

            I never thought of that. Can you try to use this compiled version After that, try to copy 2 files and cv2.pyd from buildpython2.7 to folder Python27Libsite-packages
            Hope you can run it this time =)

          • Chris

            Still getting that same error when using the precompiled version. Do I need to extract it to a particular directory?

  • triveni

    I am doing project on object tracking using camera with robot navigation,
    ive been trying to create a simple program with python2.7 which utilises opencv2.2 to get a video feed from my webcam and display it on the screen. I know im partly there because the window is created and the light on my webcam flicks on, but it just doesnt seem to show anything in the window. hopefully you can explain what im doing wrong.
    this was the program implemented,
    import cv
    3 cv.NamedWindow(“camera”, 1)
    4 capture = cv.CaptureFromCAM(0)
    6 while True:
    7 img = cv.QueryFrame(capture)
    8 cv.ShowImage(“camera”, img)
    9 if cv.WaitKey(10) == 27:
    10 break
    11 cv.DestroyWindow(“camera”)
    I havent installed any library for webcam/usb logitech cam……so please suggest if any libraries are required to be installed.

  • Frank

    Thanks for your help Gia!

    • Gia Thuy

      You’re welcome Frank =)

  • djdane
  • Henry

    Hi !

    First of all, thank you very much! This little tutorial has been of great help, since the official install guide ( ) doesn’t really explain, it just mentions all generally and one can get easily lost.

    Ok, here I go (I’m a total noob by the way, so please go easy on me :P ):

    I have downloaded and installed OpenCV 2.3.1 (OpenCV-2.3.1-win-superpack.exe), you already mentioned in a response to Chris that the windows superpack version is already compiled, which i didn’t knew at all and was confused about “intall” meaning to download source and compile.

    I’m using Windows 7 SP1 32bit, followed your steps through step3, and everything went better than expected. Now, I didn’t understand in step 4, when setting the path in environment variables, i have no “C:/OpenCV2.2/bin” folder on my opencv install, it doesn’t exist. Neither does exist “C:/OpenCV2.2/Python2.7/Lib/site-packages”.

    Does this have to do with the fact that I’m using Open CV 2.3.1, and this version does not have those folders in that order? I have about 3 days going on on this.

    However, i tried your finall response to Chris,
    “Gia Thuy
    September 18, 2011 at 9:39 AM

    I never thought of that. Can you try to use this compiled version After that, try to copy 2 files and cv2.pyd from buildpython2.7 to folder Python27Libsite-packages
    Hope you can run it this time =) ”

    I copied the 2 files just as you said, and your code to show the webcam worked. But I’m still wondering about the Python and OpenCV path, as i have not created them. Cheking in the windows cmd.exe, Python and OpenCV paths don’t exist. Should I add the paths, or what would you suggest?

    • Gia Thuy

      Hi Henry,
      Step 4 is only applicable for OpenCV2.2. I forgot to note that. Sorry about that. For this step, you need to copy 2 files “” and “cv.pyd” from folder “C:opencvbuildpython2.7″ to “C:Python27Libsite-packages” (assume that your directory as like this). Hope this helps.

      • Henry

        Oh that explains it. Thank you very much for your help! :D

  • George

    I did everything but when I try to run 2.3.1 version of, I get…
    Traceback (most recent call last):
    File “”, line 10, in
    import as cv
    ImportError: No module named

    • Gia Thuy

      Hi George, have you copied 2 files and cv.pyd from C:opencvbuildpython2.7 to the folder C:Python27Libsite-packages (assuming that your install directories are the same)?

      • Dom

        When copying the files across i just seem to get this error
        Traceback (most recent call last):
        File “C:/Users/Dom/Documents/Python Practice/Open”, line 2, in
        import cv
        ImportError: DLL load failed: The specified module could not be found.

        • Dom

          When i down load the file there is only “cv.lib” and “cv.pyd” no””

      • domenico

        Hi Gia Thuy,thanks a lot for your excellent tutorial,it has been of great help for me.

        I had the same problem “ImportError: num py.core.multiarray failed to import” when using openCV 2.2 + python2.7 + visual studio 2008.

        The problem was,I guess, that I used OpenCV-2.2.0-win32-vs2010.exe that should not be compatible with vs2008.

        I solved the issue installing the following :

        OpenCV-2.1.0-win32-vs2008 + ActivePython- + vs2008 + numpy-1.6.1-win32-superpack-python2.6 + scipy-0.9.0-win32-superpack-python2.6

        With the above your sample code for camera image processing works fine (no need to copy files “” and “cv.pyd”)

        Hope this helps,thanks again very much for your support !

        • Gia Thuy

          Thank you for your information! =)

  • Tae

    Thanks for your explanations.

  • mahesh

    Thanks man it helped me alot

  • deepender

    cool man hats off to you , initally i was trying visual studio but was not sucessfull from last 5 days then switch to python it is really nice. once again thanks to you.

  • Dov

    Thank you very much! I couldn’t setup exactly with other site’s setup information, but your way worked well!

    In my case, I installed Python 2.7 x64 version, so I couldn’t install numpy and scipy. If anyone installed Python like me, then you can re-install Python x86 version and it will be worked very well.

  • madhu

    hi Luu Gia Thuy ,
    i am using python 2.6 and opencv 2.0 to code my academic project.
    i have problem in face detection and face recognition code which extracts detected faces from jpeg image.
    so, can you please give me a code for face detection and recognition.the code can load a jpeg image draws a rectangular box for detected faces in the image and it should save the face detected faces in another image. another is the face recognised image can be compared with one another image. please i am suffering lot from this problem i hope u will give solution.

    • Gia Thuy

      For face detection, I think you can find it in OpenCV samples folder. Sorry but don’t have code for face recognition.

      • madhu

        thank u can u tell me the path specificaly

  • Haris

    i am getting the following error,
    RuntimeError: module compiled against API version 6 but this version of numpy is 4

    Traceback (most recent call last):
    File “”, line 1, in
    import cv
    File “”, line 1, in
    from import *
    ImportError: numpy.core.multiarray failed to import

    i am using python2.7, openCV2.3.1, numpy for 2.7

  • Ashish

    Thank you for this post! The instructions on OpenCV’s website are so confusing, I would have been lost without your instructions. I’ve finally got Opencv 2.3 running with Python.


    • Gia Thuy

      You’re welcome Ashish! =)

  • madhu

    plz reply me i dont know why i didnt get reply from you

  • Samir

    Has anybody tried to use OpenCV on Google App Engine ( Python ) ? Is it possible ?

  • David

    This made setting up opencv a breeze. Thanks for posting.

  • husam

    Although I have python 2.6 but your tutorial was very helpful to me.
    Thank you very much for your explanations.

  • Shiv

    Thanks. Great work!

  • Virgil Stokes

    I am trying to get OpenCV up and running with Python 2.7 (or 2.6) on a Windows Vista (32-bit) platform; but, have not been able to complete the following from your setup instructions

    Step 2: Download and install OpenCV 2.2 from Note that this version only supports Python 2.7 (not 3.x).

    I actually have downloaded both 2.2 and 2.3 and I have NumPy, SciPy, CMake, etc. all installed. I also have cygwin (for C/C++ compilers). I have unzipped the *.exe for OpenCV; but, then I start to have problems.

    Please provide some more detail on the actual installation of OpenCV (either 2.2 or 2.3) for either Python 2.6 or 2.7.

    Best regards,
    –V. Stokes
    Uppsala, Sweden

  • Nilson Morais

    Tested on Linux and it works fine. thanks

  • Raymond

    Luu Gia Thuy! Thanks so much! This is great.

    However, I tried doing this with a later version of OpenCV. 2.4 and the directory structure has changed completely.

    I have copied the files from “modules/python/src2/” to python library.
    However where is the bin files? There are like 6-8 different bins, GPU, x86,vc9, vc10, mingw?

    which one do I set the PATH variables to? Please help, and thanks in advance

    • John V. Busch

      Thanks Raymond!

      Been at this for hours as I decided to upgrade my version of OpenCV on Windows 7-64(not my call, it’s for a client).

      This helped immensely, although I am still having issues with these Python modules loading the appropriate DLLs, your comment has brought me much closer. This should be included in the Install instructions.

  • Ahirnish

    You just made everything go so smoothly!
    Thanks for such awesome post.

  • sobhan

    thank you alot :-)
    do you tink i can install opencv on a 64 bit cumputer

  • Ahirnish

    Hi again,
    I wanted to ask if it is possible to adjust brightness or zoom-in from the code above? Are there any functions available in the OpenCV for this specific purpose?


  • Richard

    Many thanks – worked first time!

  • Adam

    Oh man, have been trying forever to get this to work, and it’s so simple. Thanks for the guide!

  • Deepak

    Extremely Helpful. Thanks a ton :)

  • AMIT

    simple and effective…

    it worksssssssssssssss

  • Behzad

    Thanks man, you are great.

  • Roberto

    I can not get video from file, could you indicate waht is wrong?
    the programm below get a None frame, the file exist on the path, open the window but without image.
    I print : capture= and frame= None
    The programm is :
    import sys
    import cv

    if __name__ == ‘__main__':

    cv.NamedWindow(“Example2″, 1)

    print ‘capture=’,capture
    loop = True
    frame = cv.QueryFrame(capture)
    print ‘frame=’,frame
    if (frame == None):
    cv.ShowImage(“Example2″, frame)
    char = cv.WaitKey(33)
    if (char != -1):
    if (ord(char) == 27):
    loop = False


  • DaveS

    You da man– this was described perfectly! Thanks so much!

  • Alatec

    When I started the script for the first time, something popped up asking me to choose between 2 options for cameras even though I only had 1 plugged in. I chose the wrong one and now it won’t work and the options won’t pop up. How do I change my choice?

  • SBK

    For Open CV 2.4.x (cv2) the above code will not work. Change as follows
    import cv2

    cv2.namedWindow(“camera”, 1)
    webCamHndlr = cv2.VideoCapture(0)

    while True:
    ret,img =
    cv2.imshow(“camera”, img)
    if cv2.waitKey(10) == 27:

    Tested with Python 2.7 (32 bit for the sake of numpy and scipy) numpy 1.7, scipy 0.11, Open CV 2.4.4, on Windows 8 on a MBP :)

    • Timur

      import as cv

    • Tatya

      i am currently working with open cv 2.2 and have problem of blackscreen on my windows7 system..
      i tried several ways to table this problem but was not successful.. can you provide me best way to do this?

  • Tatya

    Im getting Black screen when i start running camera program… error only black screen….

  • You Rock

    Thank you so much man, I thought I would never get OpenCV running. This really means a lot to me. Thanks so much bro!! :D :D :D

  • Lance

    Thank you do much for writing this. I was blocked with installations of more recent versions of Python and OpenVC and simply backed out, followed your well written instructions exactly and am up and running…

  • rbaral

    Thanks for the post, thanks SBK, the way you mentioned worked for me