Can't install OpenCV + Python in Windows

asked 2018-04-19 17:19:09 -0500

I've followed the documentation here:

The documentation lists two methods, but I can't get either of them to work. The first method, installing from prebuilt binaries, results in the error ImportError: DLL load failed: The specified module could not be found. when trying to import cv2.

The second method, building from source, is the desired method because my end goal is to tweak the build configuration for our use case (otherwise I'd just use But I can't get this method to work either. The first 7 instructions go fine, here's my CMake output:

The next couple instructions are fine. At the 10th instruction there are no BUILD_opencv_gpu* entries, and there is no BUILD_opencv_python entry, but there is a BUILD_opencv_python_bindings_generator entry already enabled, so I just continue on. At instruction 12 there's no single PYTHON_* set of entries, there's two sets consisting of PYTHON2_* and PYTHON3_*. I'm using a fresh install of Python3, and CMake correctly filled in PYTHON3_EXECUTABLE, PYTHON3_NUMPY_INCLUDE_DIRS, and PYTHON3_PACKAGES_PATH, so I manually enter in PYTHON3_INCLUDE_DIR and PYTHON3_LIBRARY (there's a weird PYTHON3_INCLUDE_DIR2leftover so I leave that blank). I run CMake Configure again, and then Generate, here's the output:

I then follow the remaining instructions, everything builds successfully, no errors or warnings, but then get the error ModuleNotFoundError: No module named 'cv2' when trying to import cv2.

I'm not seeing any python modules in the build directory, and the line OpenCV modules: > Unavailable: cudaarithm cudabgsegm cudacodec cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev java python2 python3 viz in my CMake output leads me to believe that my issue is back in the CMake configuration.

Can anybody help me build and configure the Python bindings? I'll make a PR to update the documentation once I figure out how to do it. Much appreciated.

edit retag flag offensive close merge delete


your python install is lacking c headers/libs (or cmake could not find any)

also, it seems to be 32bit. maybe your best bet is: uninstall that, and try with a new 64bit python install.

berak gravatar imageberak ( 2018-04-20 00:27:42 -0500 )edit

I'm not sure why CMake doesn't find PYTHON_LIBRARY and PYTHON_INCLUDE_DIR automatically, they're in the default location, but that appears to be expected according to the documentation and any tutorials I've come across online. The documentation says to fill in the missing Python directories manually and then run CMake Configure and Generate again, and it should use those manually entered values to complete the configuration. That's what the second CMake output I linked is showing, and you'll notice those warnings disappear. The only slight difference between what I'm seeing and what the documentation is showing is that there's no PYTHON_* anything, it's all PYTHON2_* and PYTHON3_*, I only entered values for PYTHON3.

zcreegs gravatar imagezcreegs ( 2018-04-20 07:07:02 -0500 )edit

In regards to 64 bit Python, the documentation specifically says they're using 32 bit because there are no official 64 bit binaries of Numpy, so it complicates the install process further. I can try it if you really think it will make a difference, but I haven't seen anything that would lead me to believe that's the issue.

zcreegs gravatar imagezcreegs ( 2018-04-20 07:14:11 -0500 )edit

the docs are a bit outdated, there is 64bit numpy now.

if you want to use 32bit python, at least make sure, you build 32bit opencv libs, too !

berak gravatar imageberak ( 2018-04-20 07:15:55 -0500 )edit

I believe I was building 32-bit OpenCV libs by selecting the 32-bit compiler toolchain through CMake, or is there more to it than that? I'll try everything 64-bit though and see how that goes. Thank you!

zcreegs gravatar imagezcreegs ( 2018-04-20 07:23:42 -0500 )edit