34
votes

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 win7 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 in the Lib/site-packages folder of python.

Unfortunately the suggestion of using the 32 bits version of python isn't working for me anymore as I have to handle numpy arrays too large for 32 bits.

Thanks!!!

------ UPDATE

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

Everything seems to be working fine now!

7

7 Answers

27
votes

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

4
votes

You could try installing the 32 bit version of opencv

3
votes

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.

2
votes

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

1
votes

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.

Hope that help you.

1
votes

This error can also appear when python versions are mixed:

For example if any of the DLL to be loaded has been compiled using python 2.7.16 and you try to import with python 2.7.15 this 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.

0
votes

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.

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