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

Question:

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:opencvbuildbinRelease) to the Windows PATH environment variable, restart the Python interpreter.

Everything seems to be working fine now!

Asked By: Francis

||

Answers:

You could try installing the 32 bit version of OpenCV.

Answered By: Oladapo Omonayajo

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:opencvbuildbinRelease).

Answered By: Francis

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

Answered By: lukenothing

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… :).

Answered By: Nick Brady

All you have to do is copy the cv2.pyd file from the x86 folder (C:opencvbuildpython2.7×86 for example) to C:Python27Libsite-packages, not from the x64 folder.

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%pythonpython27site-packages resolved this problem for me.

Answered By: Mark Allen

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.

Answered By: MartinD

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
Answered By: ElSheikh
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.