How to install OpenCV for Python 3.x on Windows 8.1?

10,437

Solution 1

The right way to install OpenCV 3.0.0 for Python 3.4.2 on Windows 8.1:

  1. Go to http://www.lfd.uci.edu/~gohlke/pythonlibs/#opencv
  2. Download OpenCV, NumPy and SciPy

For me I needed:

  • opencv_python-3.0.0-cp34-none-win_amd64.whl

  • scipy‑0.16.0‑cp34‑none‑win_amd64.whl

  • numpy‑1.9.2+mkl‑cp34‑none‑win_amd64.whl

    1. Download and install Visual C++ 2010 Express from http://microsoft-visual-cpp-express.soft32.com/free-download/
    2. Open a command prompt as administrator
    3. Change directory like "cd C:\Users\me\Downloads" to where I downloaded the file
    4. Copy and paste this: "python -m pip install -U pip"
    5. Then install the libraries. Copy and paste this:

      pip install opencv_python-3.0.0-cp34-none-win_amd64.whl

      pip install scipy‑0.16.0‑cp34‑none‑win_amd64.whl

      pip install numpy‑1.9.2+mkl‑cp34‑none‑win_amd64.whl

That is all.

-------------> Tricky Python <------------

Solution 2

You can use pip to install the whl file like:

pip install path/to/whlfile

Yes, you are right. Gohlke's package used to be an EXE file file, but OpenCV is now distributed as whl. Sorry I missed that.

Here is a similar Stack Overflow question: How do I install a Python package with a .whl file?

Share:
10,437
Jose Fernando Gomez Davila
Author by

Jose Fernando Gomez Davila

Updated on June 04, 2022

Comments

  • Jose Fernando Gomez Davila
    Jose Fernando Gomez Davila almost 2 years

    I am trying to install and run OpenCV 3.0.0 for python 3.4.2, in Windows 8.1.

    1. I have downloaded the OpenCV file from http://opencv.org/.
    2. Extracted the folder in C:.
    3. I went to SystemAdvanced System SettingsAdvancedEnvironment variables
    4. Add a new variable in "User Variables frame". Name it "OPENCV_DIR" with value "c:\opencv\build\x64\vc12".
    5. Add this at the end of Path: ";C:\opencv;C:\opencv\build;%OPENCV_DIR%\bin"
    6. I took the archive "cv2.pyd" from "C:\opencv\build\python\2.7\x64" and copied it.
    7. I pasted it in C:\Python34\Lib\site-packages.

    Finally I went to the IDLE of Python 3.4.2 and wrote (this happened before copying the fine into the Python directory):

    import cv2
    Traceback (most recent call last):
      File "<pyshell#0>", line 1, in <module>
        import cv2
    ImportError: No module named 'cv2'
    

    And this happened after I copied the file into the Python directory:

    import cv2
    Traceback (most recent call last):
      File "<pyshell#3>", line 1, in <module>
        import cv2
    ImportError: DLL load failed: The specified module could not be found.
    

    What am I doing wrong? I not sure whether I should install CMake and NumPy.

    Also I am not sure how to use this webpage (recommended by so many people), http://www.lfd.uci.edu/~gohlke/pythonlibs/#opencv, where many Python libraries are available, including PIL, OpenCV, VideoCapture, etc...

    I want to try to run the examples from:

    OK, I should not use the file from Python 2.7 in Python 3.4. It won't work. I deleted it, so as otterb suggested, I just installed the opencv_python‑3.0.0‑cp34‑none‑win_amd64.whl package from http://www.lfd.uci.edu/~gohlke/pythonlibs/#opencv, but this file is an .whl not an EXE file, and according to How do I install a Python package with a .whl file? I followed these steps:

    1. Go to the command prompt

    2. Change directory with "cd" to where I downloaded the file

    3. And write "pip install opencv_python-3.0.0-cp34-none-win_amd64.whl"

    But I got the following message in red:

    Installing collected packages: opencv-python
    Cleaning up...
      Exception:
    Traceback (most recent call last):
      File "C:\Python34\lib\shutil.py", line 370, in _rmtree_unsafe
        os.unlink(fullname)
    PermissionError: [WinError 32] The process cannot access the file because it     is
    being used by another process:     'C:\\Users\\JOSEFG~1\\AppData\\Local\\Temp\\pip-n
    kyo0c6u-build\\opencv_python-3.0.0.data\\data\\Lib\\site-packages\\cv2.pyd'
    
    During handling of the above exception, another exception occurred:
    
    Traceback (most recent call last):
      File "C:\Python34\lib\site-packages\pip\commands\install.py", line 283, in     run
    
        requirement_set.install(install_options, global_options,     root=options.root_p
    ath)
      File "C:\Python34\lib\site-packages\pip\req.py", line 1444, in install
        requirement.remove_temporary_source()
      File "C:\Python34\lib\site-packages\pip\req.py", line 761, in     remove_temporary
    _source
        rmtree(self._temp_build_dir)
      File "C:\Python34\lib\site-packages\pip\util.py", line 43, in rmtree
        onerror=rmtree_errorhandler)
      File "C:\Python34\lib\shutil.py", line 477, in rmtree
        return _rmtree_unsafe(path, onerror)
      File "C:\Python34\lib\shutil.py", line 367, in _rmtree_unsafe
        _rmtree_unsafe(fullname, onerror)
      File "C:\Python34\lib\shutil.py", line 367, in _rmtree_unsafe
        _rmtree_unsafe(fullname, onerror)
      File "C:\Python34\lib\shutil.py", line 367, in _rmtree_unsafe
        _rmtree_unsafe(fullname, onerror)
      File "C:\Python34\lib\shutil.py", line 367, in _rmtree_unsafe
        _rmtree_unsafe(fullname, onerror)
      File "C:\Python34\lib\shutil.py", line 372, in _rmtree_unsafe
        onerror(os.unlink, fullname, sys.exc_info())
      File "C:\Python34\lib\site-packages\pip\util.py", line 53, in rmtree_errorhandler
        (exctype is PermissionError and value.args[3] == 5) #python3.3
    IndexError: tuple index out of range
    
    During handling of the above exception, another exception occurred:
    
    Traceback (most recent call last):
      File "C:\Python34\lib\shutil.py", line 370, in _rmtree_unsafe
        os.unlink(fullname)
    PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'C:\\Users\\JOSEFG~1\\AppData\\Local\\Temp\\pip-nkyo0c6u-build\\opencv_python-3.0.0.data\\data\\Lib\\site-packages\\cv2.pyd'
    
    During handling of the above exception, another exception occurred:
    
    Traceback (most recent call last):
      File "C:\Python34\lib\site-packages\pip\basecommand.py", line 122, in main
        status = self.run(options, args)
      File "C:\Python34\lib\site-packages\pip\commands\install.py", line 302, in run
    
        requirement_set.cleanup_files(bundle=self.bundle)
      File "C:\Python34\lib\site-packages\pip\req.py", line 1319, in cleanup_files
        req.remove_temporary_source()
      File "C:\Python34\lib\site-packages\pip\req.py", line 761, in remove_temporary_source
        rmtree(self._temp_build_dir)
      File "C:\Python34\lib\site-packages\pip\util.py", line 43, in rmtree
        onerror=rmtree_errorhandler)
      File "C:\Python34\lib\shutil.py", line 477, in rmtree
        return _rmtree_unsafe(path, onerror)
      File "C:\Python34\lib\shutil.py", line 367, in _rmtree_unsafe
        _rmtree_unsafe(fullname, onerror)
      File "C:\Python34\lib\shutil.py", line 367, in _rmtree_unsafe
        _rmtree_unsafe(fullname, onerror)
      File "C:\Python34\lib\shutil.py", line 367, in _rmtree_unsafe
        _rmtree_unsafe(fullname, onerror)
      File "C:\Python34\lib\shutil.py", line 367, in _rmtree_unsafe
        _rmtree_unsafe(fullname, onerror)
      File "C:\Python34\lib\shutil.py", line 372, in _rmtree_unsafe
        onerror(os.unlink, fullname, sys.exc_info())
      File "C:\Python34\lib\site-packages\pip\util.py", line 53, in rmtree_errorhandler
        (exctype is PermissionError and value.args[3] == 5) #python3.3
    IndexError: tuple index out of range
    
      Storing debug log for failure in C:\Users\me\pip\pip.log
    

    Which means after reading it carefully that the file is unsafe to install on my PC.

    Also it seems that pip should be the latest one to run the previous command according to How do I install pip on Windows?. Then I just upgrade pip in the command prompt with

    python -m pip install -U pip
    

    Now I have version 7.1. I know it by writing:

    pip --version
    

    Then I tried again

    pip install opencv_python-3.0.0-cp34-none-win_amd64.whl
    

    But it seems that it is working as I am getting this message:

    C:\Users\Jose F Gomez D\Downloads>pip install opencv_python-3.0.0-cp34-none-win_amd64.whl
    Requirement already satisfied (use --upgrade to upgrade): opencv-python==3.0.0 from file:///C:/Users/Jose%20F%20Gomez%20D/Downloads/opencv_python-3.0.0-cp34-none-win_amd64.whl in c:\python34\lib\site-packages
    

    then I write in the python shell "import cv2" but there is a new error

    ImportError: numpy.core.multiarray failed to import
    Traceback (most recent call last):
      File "<pyshell#0>", line 1, in <module>
        import cv2
    ImportError: numpy.core.multiarray failed to import
    

    so I installed the numpy (and scipy just in case) library with

    pip install numpy
    pip install scipy
    

    But it is not working as it requires C++

    error microsoft visual c++ 10 is required

    I tried to find "Visual C++ 2010 Express" from the webpage of Visual Studio at https://www.visualstudio.com/, but there is no link to "Microsoft Visual C++ 2010 express", so I went to page http://microsoft-visual-cpp-express.soft32.com/free-download/, downloaded the file, executed it, and now I got C++.

    Again I went to the command prompt and tried to install NumPy and SciPy and it did not work. There is an error in the path:

    ValueError: ['path']
    

    OK, now I tried to install the NumPy and SciPy packages from:

    http://sourceforge.net/projects/numpy/files/NumPy/

    But it did not work, so I went to Gohike, http://www.lfd.uci.edu/~gohlke/pythonlibs/#numpy, and installed NumPy and SciPy from the command prompt in administrator mode:

    pip install scipy‑0.16.0‑cp34‑none‑win_amd64.whl
    pip install numpy‑1.9.2+mkl‑cp34‑none‑win_amd64.whl
    

    Now I tried to run cv, but nothing. Uups, it's cv2, and it is working. I will write the answer next.