Ask Your Question

Compiling/Installing from source -- python import cv2 module could not be found

asked 2016-12-07 11:02:54 -0500

Ondrej gravatar image

updated 2016-12-07 14:02:52 -0500

I'm receiving the error "ImportError: DLL load failed: The specified module could not be found." when I type "import cv2" in python (2.7 32 bit). Typing sys.path reveals .../Anaconda2/lib/site-packages is on the path and the file cv2.pyd exists in this directory.

image description

After poking around online I see many people talking about not cv2.pyd (but it's not clear to me that this isn't just an operating system difference since they're all running some flavor of linux). When I perform a search of anaconda sub-directories no exists. When I search the opencv build folder, again no Where did I go wrong? How can I get python to find opencv?

EIDT: It appears the .so file extension is indeed just due to different operating systems. In this thread Andrew explains "An .so file is a compiled library file. It stands for "Shared Object" and is analogous to a Windows DLL." In the error given by python it appears as though it's looking for a .DLL file. So this sounds like the sample type of file just for different OS. Further digging indicates that a .pyd file (which I have) is essentially a .DLL file. This information can be found here. So why is python not finding it? Is it expecting a .DLL file extension instead of .pyd?

Background: Windows 7 Enterprise SP1 64 bit

I started by uninstalling all previous flavors of python, then downloading and installing Anaconda 4.2.0 -- python 2.7 (32 bit)

Then I downloaded opencv and the additional contrib packages

Following instructions from the opencv website I downloaded Microsoft Visual Studio 2015 and cmake.

Created a directory "C:\OpenCV\Build" and ran cmake from within a Cygwin terminal (inside the Build directory) with the command "cmake -DOPENCV_EXTRA_MODULES_PATH=C:\OpenCV\opencv_contrib-master\modules C:\OpenCV\opencv-master". This appeared to work fine and produced a bunch of files and folders in the build directory including one called "OpenCV.sln". NOTE: This step is slightly different than the cmake-gui install instructions I was following here. I got this alternate command line build instructions from the opencv_contrib download page.

I opened OpenCV.sln with Visual Studio, switched the build mode from "debug" to "release", selected "ALL_BUILD" under "CMakeTargets" and ran Build>Build ALL_BUILD (from the drop down Build menu at the top). This also seemed to work fine and produced no errors.

image description

Finally, I selected "INSTALL" under "CMakeTargets" and ran Build>Build INSTALL. Again, no errors that I could tell.

This is the last step of the install instructions according to the opencv website. However, import cv2 from python still produces the import error.

edit retag flag offensive close merge delete


  • you need to buld the *INSTALL" project, to generate the python wrappers, and copy those to the desired install location.
  • if you run into trouble, please adda text version of the cmake output to your question, this might help to find obvious misses.
berak gravatar imageberak ( 2016-12-07 11:25:49 -0500 )edit

@berak Thanks for the quick response! I built the INSTALL project (not the ALL_BUILD again, as I originally posted). What file extensions am I looking for to signify that those are python wrappers?

Ondrej gravatar imageOndrej ( 2016-12-07 11:39:55 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2016-12-09 11:10:59 -0500

Ondrej gravatar image

updated 2016-12-09 11:16:47 -0500

Well, I found a work-around. While this doesn't answer the specific question I had about getting the packages compiled from source, it did get me a working install of opencv and the contrib packages.

Note: This is for windows only!

Following instructions from here, I downloaded the appropriate (for the python version I'm running) precompiled whl file from Christoph Gohlke's page. I put this file in my python scripts directory (not sure this is necessary), navigated there on the command line and entered the command "pip install fileName.whl".

That's it! Hats off to Christoph.

Final note: While, in principle, entering the command "pip install opencv" should install opencv without having to manually download or compile anything, I was having problems with unavailable packages with this type of install. Some of the packages are either proprietary or are still in beta and thus, are not available through the standard install method. This is why I was trying to compile from source to begin with. The precompiled whls from Christoph's page get around having to compile yourself but also include the missing packages, at least the one I downloaded did.

I hope this is helpful to some others as well.

edit flag offensive delete link more

Question Tools

1 follower


Asked: 2016-12-07 11:02:54 -0500

Seen: 2,817 times

Last updated: Dec 09 '16