Francis
Francis

Reputation: 703

Error "ImportError: DLL load failed: %1 is not a valid Win32 application"

I have this issue where I try to import cv2 on Python and get the following error message.

>>> import cv2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: DLL load failed: %1 is not a valid Win32 application.

I do understand there are many posts about this where it is suggested that the bitness of the package is different from the Python package.

However, everything I am running is 64 bits. I am on Windows 7 64 bits, I have the winpython 2.7.3.3, 64 bits distribution, and I compiled OpenCV in 64 bits with the instruction provided here and placed the cv2.pyd DLL file in the Lib/site-packages folder of the Python interpreter.

Unfortunately, the suggestion of using the 32 bits version of Python isn't working for me any more as I have to handle NumPy arrays too large for 32 bits.


The only thing missing was to add the new NumPy binaries path (C:\opencv\build\bin\Release) to the Windows PATH environment variable, restart the Python interpreter.

Everything seems to be working fine now!

Upvotes: 36

Views: 122862

Answers (8)

ElSheikh
ElSheikh

Reputation: 329

I had the same error as the one mentioned in previous answers, but it happens only when I use pyinstaller.

I did the following in my Poetry venv:

poetry run python -m pip install pypiwin32

And it worked for me. I am using

python                            3.8.10
pyinstaller                       4.7
# Those are the versions that worked for me
pypiwin32                         223
pywin32                           303
pywin32-ctypes                    0.2.0

Upvotes: 1

MartinD
MartinD

Reputation: 125

This error can also appear when Python versions are mixed:

For example, if any of the DLL file to be loaded has been compiled using Python 2.7.16 and you try to import with Python 2.7.15 the error ImportError: DLL load failed: %1 is not a valid Win32 application. is thrown.

This is at least what I've found to be the problem in my case.

Upvotes: 2

Jhossep
Jhossep

Reputation: 551

All you have to do is copy the cv2.pyd file from the x86 folder (C:\opencv\build\python\2.7\x86\ for example) to C:\Python27\Lib\site-packages\, not from the x64 folder.

Upvotes: 1

Nick Brady
Nick Brady

Reputation: 6592

When I had this error, it went away after I my computer crashed and restarted. Try closing and reopening your IDE. If that doesn't work, try restarting your computer. I had just installed the libraries at that point without restarting PyCharm when I got this error.

I never closed PyCharm first to test, because my blasted computer keeps crashing randomly... I am working on that one, but it at least it solved this problem... little victories... :).

Upvotes: 0

lukenothing
lukenothing

Reputation: 159

Or you have to rebuild the cv2 module for Windows 64 bit.

Upvotes: 3

Francis
Francis

Reputation: 703

The ImportError message is a bit misleading because of the reference to Win32, whereas the problem was simply the OpenCV DLLs were not found.

This problem was solved by adding the path the OpenCV binaries to the Windows PATH environment variable (as an example, on my computer this path is: C:\opencv\build\bin\Release).

Upvotes: 29

Oladapo Omonayajo
Oladapo Omonayajo

Reputation: 980

You could try installing the 32 bit version of OpenCV.

Upvotes: 5

Mark Allen
Mark Allen

Reputation: 1205

I just hit this and the problem was that the package had at one point been installed in the per-user packages directory. (On Windows.) aka %AppData%\Python. So Python was looking there first, finding an old 32-bit version of the .pyd file, and failing with the listed error. Unfortunately pip uninstall by itself wasn't enough to clean this, and at this time pip 10.0.1 doesn't seem to have a --user parameter for uninstall, only for install.

tl;dr Deleting the old .pyd from %AppData%\python\python27\site-packages resolved this problem for me.

Upvotes: 3

Related Questions