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:
import cv2
But that line throws the error shown in the title of this question.
I have OpenCV installed in C:\lib\opencv
on this 64-bit machine. I'm using 64-bit Python.
My PYTHONPATH variable: PYTHONPATH=C:\lib\opencv\build\python\2.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_core246d.dll
.
OPENCV_DIR has this value: OPENCV_DIR=C:\lib\opencv\build\x64\vc11
.
The solution at ImportError: DLL load failed: %1 is not a valid Win32 application says to add "the new opencv binaries path (C:\opencv\build\bin\Release
) to the Windows PATH environment variable". But as shown above, I already have the OpenCV binaries folder (C:\lib\opencv\build\x64\vc11\bin
) 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?
Thanks, Lars
EDIT:
I just noticed that, according to http://www.dependencywalker.com/, the cv2.pyd
in C:\lib\opencv\build\python\2.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?
For me the problem was that I was using different versions of Python in the same Eclipse project. My setup was not consistent with the Project Properties and the Run Configuration Python versions.
In Project > Properties > PyDev, I had the Interpreter set to Python2.7.11.
In Run Configurations > Interpreter, I was using the Default Interpreter. Changing it to Python 2.7.11 fixed the problem.
You can install
opencv
from official or unofficial sites.Refer to this question and this issue if you are using
Anaconda
.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:
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.
I copied
cv2.pyd
file from/opencv/build/python/2.7/x86
folder instead of from/x64
folder toC:/Python27/Lib/site-packeges
. I followed rest of the instructions provided 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.