36

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!

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Francis
  • 663
  • 1
  • 8
  • 13
  • The link is (effectively) broken: *"401: Anonymous caller does not have storage.objects.get access to the Google Cloud Storage object."* – Peter Mortensen Feb 10 '22 at 18:28
  • More details on the error: [\[SO\]: Python Ctypes - loading dll throws OSError: \[WinError 193\] %1 is not a valid Win32 application (@CristiFati's answer)](https://stackoverflow.com/a/57297745/4788546). – CristiFati Apr 26 '23 at 14:06

8 Answers8

29

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Francis
  • 663
  • 1
  • 8
  • 13
  • 8
    What is the steps taken to do this? – Exzile Jul 07 '14 at 02:20
  • What I don't get is, why doesn't the error message state the DLL that couldn't be loaded? %1 merely seems to be a placeholder for the name of the DLL. – antred Nov 19 '14 at 14:35
  • Ah ok. So you have to copy the cv2.pyd from the x64 folder, if you're on 64-bit machine to fix that error. @antred It is a placeholder. It normally gets replaced with the DLL but it doesn't work because the 32 bit fails to load on a 64 bit machine. – Alex W Apr 16 '15 at 03:26
  • @Francis: You, sir, deserve a medal! Thanks, really. – 1750028 May 05 '15 at 16:32
  • @AlexW You're saying cv2.pyd should be copied... but where to copy it? – Belial Jul 01 '15 at 10:01
  • @Belial I'm not on the machine that I installed OpenCV on right now, but I believe you have to copy that file from the 64-bit folder in `C:\opencv\build\bin\Release` into the 32-bit folder. – Alex W Jul 01 '15 at 15:02
  • There might have 32bit `.dll` files as well in your machine. Make sure to delete them if you have no use with them. – Ramesh-X Jun 10 '17 at 14:19
5

You could try installing the 32 bit version of OpenCV.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • 1
    Please mind that this issue is already solved as mentionned in the UPDATE section of the question. – Francis Jul 04 '13 at 15:28
  • 4
    @Francis once you have found a solution, please always add it as an Answer, instead of adding it to your Question. – tshepang Jul 23 '13 at 17:11
3

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
lukenothing
  • 149
  • 1
  • 6
3

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.

Mark Allen
  • 1,230
  • 1
  • 15
  • 17
  • This was my problem as well. There was an older version of python that didn't seem to be deleted properly. Had to run the System File Checker to clean it up – Rhino Mar 21 '20 at 20:12
2

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
MartinD
  • 125
  • 3
1

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jhossep
  • 531
  • 8
  • 9
1

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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ElSheikh
  • 321
  • 6
  • 28
0

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Nick Brady
  • 6,084
  • 1
  • 46
  • 71