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!

2

8 Answers 8

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

7
  • 8
    What is the steps taken to do this?
    – Exzile
    Jul 7, 2014 at 2: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, 2014 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, 2015 at 3:26
  • @Francis: You, sir, deserve a medal! Thanks, really.
    – 1750028
    May 5, 2015 at 16:32
  • @AlexW You're saying cv2.pyd should be copied... but where to copy it?
    – Belial
    Jul 1, 2015 at 10:01
5

You could try installing the 32 bit version of OpenCV.

2
  • 1
    Please mind that this issue is already solved as mentionned in the UPDATE section of the question.
    – Francis
    Jul 4, 2013 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, 2013 at 17:11
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.

1
  • 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, 2020 at 20:12
3

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

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.

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.

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.