I have a situation very much like the one at ImportError: DLL load failed: %1 is not a valid Win32 application, but the answer there isn’t working for me.
My Python code says:
But that line throws the error shown in the title of this question.
I have OpenCV installed in
C:libopencv on this 64-bit machine. I’m using 64-bit Python.
My PYTHONPATH variable:
PYTHONPATH=C:libopencvbuildpython2.7. This folder contains
cv2.pyd and that’s all.
My PATH variable:
Path=%OPENCV_DIR%bin;... This folder contains 39 DLL files such as
OPENCV_DIR has this value:
The solution at ImportError: DLL load failed: %1 is not a valid Win32 application says to add “the new opencv binaries path (
C:opencvbuildbinRelease) to the Windows PATH environment variable”. But as shown above, I already have the OpenCV binaries folder (
C:libopencvbuildx64vc11bin) in my PATH. And my OpenCV installation doesn’t have any Release folders (except for an empty one under build/java).
Any ideas as to what’s going wrong? Can I tell Python to verbosely trace the loading process? Exactly what DLL’s is it looking for?
I just noticed that, according to http://www.dependencywalker.com/, the
C:libopencvbuildpython2.7 is 32-bit, whereas the machine and the Python I’m running are 64-bit. Could that be the problem? And if so, where can I find a 64-bit version of cv2.pyd?
you can find any python libs from here
Please check if the python version you are using is also 64 bit. If not then that could be the issue. You would be using a 32 bit python version and would have installed a 64 bit binaries for the OPENCV library.
Wow, I found yet another case for this problem. None of the above worked. Eventually I used python’s ability to introspect what was being loaded. For python 2.7 this means:
import imp imp.find_module("cv2")
This turned up a completely unexpected “cv2.pyd” file in an Anaconda DLL directory that wasn’t touched by multiple uninstall/install attempts. Python was looking there first and not finding my good installation. I deleted that cv2.pyd file and tried imp.find_module(“cv2”) again and python immediately found the right file and cv2 started working.
So if none of the other solutions work for you, make sure you use python introspection to see what file python is trying to load.
In my case, I have 64bit python, and it was lxml that was the wrong version–I should have been using the x64 version of that as well. I solved this by downloading the 64-bit version of lxml here:
This was the simplest answer to a frustrating issue.
I just had this problem, it turns it was just because I was using x64 version of the opencv file. Tried the x86 and it worked.
I had the same problem. Here’s what I did:
I downloaded pywin32 Wheel file from here, then
I uninstalled the pywin32 module. To uninstall execute the following command in Command Prompt.
pip uninstall pywin32
Then, I reinstalled pywin32. To install it, open the Command Prompt in the same directory where the pywin32 wheel file lies. Then execute the following command.
pip install <Name of the wheel file with extension>
Wheel file will be like: piwin32-XXX-cpXX-none-win32.whl
It solvs the problem for me. You may also like to give it a try. Hope it work for you as well.
If your build-system (CMake in my case) copies the file from
<name>.pyd, you will get this error if the original file wasn’t actually a dll. In my case, building shared libraries got switched off, so the underlying file was actually a
I discovered this error by loading the
pyd file in DependencyWalker and finding that it wasn’t valid.
cv2.pyd file from
/opencv/build/python/2.7/x86 folder instead of from
/x64 folder to
C:/Python27/Lib/site-packeges. I followed rest of the instructions provided here.
Added by someone else, not verified: I also copy file
cv2.pyd to folder
C:/Python27/Lib/site-packages/cv2. It works.