130

I have a situation very much like the one at Error "ImportError: DLL load failed: %1 is not a valid Win32 application", but the answer there isn't working for me.

My Python code says:

import cv2

But that line throws the error shown in the title of this question.

I have OpenCV installed in C:\lib\opencv on this 64-bit machine. I'm using 64-bit Python.

My PYTHONPATH variable: PYTHONPATH=C:\lib\opencv\build\python\2.7. This folder contains cv2.pyd and that's all.

My PATH variable: Path=%OPENCV_DIR%\bin;... This folder contains 39 DLL files such as opencv_core246d.dll.

OPENCV_DIR has this value: OPENCV_DIR=C:\lib\opencv\build\x64\vc11.

The solution at Error "ImportError: DLL load failed: %1 is not a valid Win32 application" says to add "the new opencv binaries path (C:\opencv\build\bin\Release) to the Windows PATH environment variable". But as shown above, I already have the OpenCV binaries folder (C:\lib\opencv\build\x64\vc11\bin) in my PATH. And my OpenCV installation doesn't have any Release folders (except for an empty one under build/java).

What's going wrong? Can I tell Python to verbosely trace the loading process? Exactly what DLL files is it looking for?

I noticed that, according to http://www.dependencywalker.com/, the cv2.pyd in C:\lib\opencv\build\python\2.7 is 32-bit, whereas the machine and the Python I'm running are 64-bit. Could that be the problem? And if so, where can I find a 64-bit version of cv2.pyd?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
LarsH
  • 27,481
  • 8
  • 94
  • 152
  • 10
    click [here](http://www.lfd.uci.edu/~gohlke/pythonlibs/#opencv) and find the 64-bit opencv – Kill Console Sep 26 '13 at 05:41
  • 1
    @KillConsole: thanks, I'll try it. Since the install package I downloaded had both x86 and x64 subfolders, I assumed it included everything there was to have for 64-bit. Apparently not. Hooray, that fixed it! Now we're on to "ImportError: numpy.core.multiarray failed to import", so I'll try the same site for 64-bit numpy. If you make your comment into an answer, I'll upvote and accept it. – LarsH Sep 26 '13 at 12:52
  • which Python version did you use ? (not in terms of processor bits version) –  Feb 03 '15 at 12:49
  • 1
    @Begueradj: 2.7, if that's what you're asking. – LarsH Feb 03 '15 at 14:11
  • Hi all, I encountered the same issue (after copying `x64/cv2.pyd`, because my PC is 64-bit) , however then I tried copying `x86/cv2.pyd` and it worked. Is this because my Python installation 32-bit (I'm not sure it is but it's the only way I can make sense of this) ? – jeff Sep 11 '15 at 12:08
  • For me it happened with pycrypto, and all I had to do is to reinstall it. Problem solved. – Ghasan غسان May 01 '16 at 17:30
  • 2
    I had the same problem and none of the below solutions helped me, so it turned out that my `PYTHONPATH` used to contain the entries which the install of SimpleCV library has left there. Despite I have already uninstalled the SimpleCV, the `PYTHONPATH` was not restored and was pointing to some directories with conflicting versions of OpenCV left by SimpleCV install. Solution: clear the `PYTHONPATH`. – Anton Daneyko Jan 29 '20 at 20:21
  • 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:07

24 Answers24

88

Unofficial Windows Binaries for Python Extension Packages

You can find any Python libraries from here.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Kill Console
  • 1,933
  • 18
  • 15
  • this really helped. I am not sure what was wrong. I had a version of opencv, anaconda3, python3. Installed opencv_python-3.3.0+contrib-cp35-cp35m-win_amd64 from above package list and was able to import cv2 successfully after hours of struggle. Thanks a ton. – emeralddove Sep 23 '17 at 08:48
  • This link seem dead. – softyoda yoann May 30 '23 at 16:14
45

Please check if the Python version you are using is also 64 bit. If not then that could be the issue. You would be using a 32-bit Python version and would have installed a 64 bit binaries for the OpenCV library.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Clive Verghese
  • 469
  • 4
  • 4
20

Wow, I found yet another case for this problem. None of the above worked. Eventually I used python's ability to introspect what was being loaded. For Python 2.7, this means:

import imp
imp.find_module("cv2")

This turned up a completely unexpected "cv2.pyd" file in an Anaconda DLL directory that wasn't touched by multiple uninstall/install attempts. Python was looking there first and not finding my good installation. I deleted that cv2.pyd file and tried imp.find_module("cv2") again and python immediately found the right file and cv2 started working.

So if none of the other solutions work for you, make sure you use Python introspection to see what file Python is trying to load.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ezekiel Kruglick
  • 4,496
  • 38
  • 48
  • 4
    I had an issue were it was trying to run a version of the file in a different folder completely. This solution here helped me figure out what was really going on. Thanks! – Emad Y Jan 02 '17 at 12:49
  • 3
    This is an excellent answer, thanks! I ran into an issue with PyDev where it was loading "ctypes" from an incompatible installation of python, and I realized that I needed to set up my interpreter to use WinPython, which fixed it. – eacousineau Feb 08 '17 at 15:58
7

In my case, I have 64-bit Python, and it was lxml that was the wrong version--I should have been using the x64 version of that as well. I solved this by downloading the 64-bit version of lxml here:

https://pypi.python.org/pypi/lxml/3.4.1

lxml-3.4.1.win-amd64-py2.7.exe

This was the simplest answer to a frustrating issue.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Alkanshel
  • 4,198
  • 1
  • 35
  • 54
6

I just had this problem. It turns out it was just because I was using an 64-bit version of the OpenCV file. I tried the x86 and it worked.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
olive_tree
  • 1,417
  • 16
  • 23
3

I had the same problem. Here's what I did:

  1. I downloaded the pywin32 wheel file from here, then

  2. I uninstalled the pywin32 module. To uninstall, execute the following command in a command prompt.

    pip uninstall pywin32

  3. Then, I reinstalled pywin32. To install it, open the command prompt in the same directory where the pywin32 wheel file lies. Then execute the following command.

    pip install <Name of the wheel file with extension> Wheel file will be like: piwin32-XXX-cpXX-none-win32.whl

It solves the problem for me.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Abhishek Kashyap
  • 3,332
  • 2
  • 18
  • 20
2

I copied cv2.pyd file from /opencv/build/python/2.7/x86 folder instead of from /x64 folder to C:/Python27/Lib/site-packeges. I followed rest of the instructions provided here.

Added by someone else, not verified: I also copy file cv2.pyd to folder C:/Python27/Lib/site-packages/cv2. It works.

Autonomous
  • 8,935
  • 1
  • 38
  • 77
2

For me the problem was that I was using different versions of Python in the same Eclipse project. My setup was not consistent with the Project Properties and the Run Configuration Python versions.

In menu ProjectPropertiesPyDev, I had the Interpreter set to Python 2.7.11.

In Run ConfigurationsInterpreter, I was using the Default Interpreter. Changing it to Python 2.7.11 fixed the problem.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Al Lelopath
  • 6,448
  • 13
  • 82
  • 139
2

If your build system (CMake in my case) copies the file from <name>.dll to <name>.pyd, you will get this error if the original file wasn't actually a DLL file. In my case, building shared libraries got switched off, so the underlying file was actually a *.lib.

I discovered this error by loading the pyd file in Dependency Walker and finding that it wasn't valid.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
MatrixManAtYrService
  • 8,023
  • 1
  • 50
  • 61
2

Update NumPy.

pip install numpy --upgrade

It works for me!

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
2

This one worked for me:

pip install -- pywin32==227
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • 2
    Welcome to Stack Overflow. Why it worked for you? Please, could you add more details to this answer? – Azametzin Jul 01 '20 at 17:53
  • 1
    Thank you! This actually worked for me. I found that I was using pywin 228, and downgrading to 227 made everything work again. Obviously something must have been added to 228 that broke backwards compatibility, but I have no idea of what. – Terje Mikal Sep 02 '20 at 08:14
1

I faced the same issue when I uninstalled and reinstalled a different version of 2.7.x of Python on my system using a 32-bit Windows Installer. I got the same error on most of my import statements.

I uninstalled the newly installed Python, downloaded a 64-bit Windows installer, reinstalled Python again, and it worked.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user3233042
  • 39
  • 1
  • 2
1

So I had problems installing vtk under Windows (as I use Python 3.7, there isn't any binary available so far. Just for older Python versions pip install vtk is not working)

I did wrote Python in my cmd:

Python 3.7.3 on win32

So I now know I have Python 3.7.3 running on a 32 bit.

I then downloaded the correct wheel at VTK‑8.2.0‑cp37‑cp37m‑win32.whl

Next I installed that wheel:

pip install VTK-8.2.0-cp37-cp37m-win32.whl

Then I tested it and it worked:

python
import vtk
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
sqp_125
  • 538
  • 6
  • 21
1

I experienced the same problem while trying to write code concerning speech-to-text.

The solution was very simple. Uninstall the previous pywin32 using the pip method:

pip uninstall pywin32

The above will remove the existing one which is by default for 32 bit computers. And install it again using

pip install pywin32

This will install the one for the 64 bit computer which you are using.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Joel
  • 487
  • 6
  • 8
1

I had a similar issue while trying to run uvicorn,
Creating a new virtual environment and reinstalling the python packages worked

0

First I copied cv2.pyd from /opencv/build/python/2.7/x86 to C:/Python27/Lib/site-packeges. The error was

"RuntimeError: module compiled against API version 9 but this version of numpy is 7"

Then I installed numpy-1.8.0-win32-superpack-python2.7.exe and OpenCV works fine.

>>> import cv2
>>> print cv2.__version__
2.4.13
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
0

You can install opencv from official or unofficial sites.

Refer to this question and this issue if you are using Anaconda.

Ramesh-X
  • 4,853
  • 6
  • 46
  • 67
0
  1. Please make sure that you have installed a Python 2.7.12 or below version. Otherwise you will definitely get this error.
  2. Make sure the Oracle client is 64 bit installed if the OS is 64 bit.
  3. Make sure the Microsoft Visual C++ compiler for Python 2.7 is 64 for bit for a 64 bit OS or 32 bit for 32 bit.

Note: If your OS is 64 bit, install all packages of 64 bit or if the OS is 32 bit, install the 32-bit package.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ranjan
  • 81
  • 7
0

It has a very simple solution. After installing opencv place

cv2.pyd from C:\opencv\build\python\2.7\ **x64** to C:\Python27\Lib\site-packages

instead of, place cv2.pyd from C:\opencv\build\python\2.7\ **x86** to C:\Python27\Lib\site-packages

4b0
  • 21,981
  • 30
  • 95
  • 142
0

I got this error when trying to import MySQLdb.

What worked for me was to uninstall Python and then reinstall it.

I got the error after installing npm (https://www.npmjs.com/get-npm). One thing it did was install Python even though I already had it.

user984003
  • 28,050
  • 64
  • 189
  • 285
0

This has worked for me. I have tried different methods, but this was my best solution.

Open a command prompt and type the following;

pip install opencv-python

(Make sure your Internet connection is on.)

After that, try importing it again.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
0

It could also be that your Anaconda version is 32 bit when it should be 64 bit.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
DaveR
  • 1,696
  • 18
  • 24
0

If you are using pycharm I go to settings -> python interpretation and click the + button and search for the name on the list of python packages there An image showing where to go when you want to install something

nathoe
  • 3
  • 4
-2

I found the solution. Maybe you can try to use the cmd window rather than the Anaconda prompt window to start your first Scrapy test.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131