ImportError: DLL load failed: %1 is not a valid Win32 application. But the DLL's are there

449,230

Solution 1

Unofficial Windows Binaries for Python Extension Packages

You can find any Python libraries from here.

Solution 2

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.

Solution 3

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.

Solution 4

In my case, I have 64-bit 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:

https://pypi.python.org/pypi/lxml/3.4.1

lxml-3.4.1.win-amd64-py2.7.exe

This was the simplest answer to a frustrating issue.

Solution 5

I just had this problem. It turns out it was just because I was using an 64-bit version of the OpenCV file. I tried the x86 and it worked.

Share:
449,230
LarsH
Author by

LarsH

Updated on February 19, 2022

Comments

  • LarsH
    LarsH over 2 years

    I have a situation very much like the one at Error "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 Error "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).

    What's going wrong? Can I tell Python to verbosely trace the loading process? Exactly what DLL files is it looking for?

    I 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?

  • LarsH
    LarsH almost 10 years
    As mentioned in the question, I was using 64-bit Python.
  • Emad Y
    Emad Y over 7 years
    I had an issue were it was trying to run a version of the file in a different folder completely. This solution here helped me figure out what was really going on. Thanks!
  • eacousineau
    eacousineau over 7 years
    This is an excellent answer, thanks! I ran into an issue with PyDev where it was loading "ctypes" from an incompatible installation of python, and I realized that I needed to set up my interpreter to use WinPython, which fixed it.
  • emeralddove
    emeralddove almost 7 years
    this really helped. I am not sure what was wrong. I had a version of opencv, anaconda3, python3. Installed opencv_python-3.3.0+contrib-cp35-cp35m-win_amd64 from above package list and was able to import cv2 successfully after hours of struggle. Thanks a ton.
  • Azametzin
    Azametzin about 4 years
    Welcome to Stack Overflow. Why it worked for you? Please, could you add more details to this answer?
  • Terje Mikal
    Terje Mikal almost 4 years
    Thank you! This actually worked for me. I found that I was using pywin 228, and downgrading to 227 made everything work again. Obviously something must have been added to 228 that broke backwards compatibility, but I have no idea of what.