By LarsH


2013-09-26 04:59:00 8 Comments

I have a situation very much like the one at 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 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).

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

Thanks, Lars

EDIT:

I just 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?

20 comments

@Mohammed Said El-Sharkawy 2020-07-01 17:02:05

This one worked with me

pip install -- pywin32==227

@Azametzin 2020-07-01 17:53:35

Welcome to Stack Overflow. Why it worked for you? Please, could you add more details to this answer?

@user12907613 2020-02-16 14:14:59

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

Open command prompt and type the following; pip install opencv-python. (make sure your internet is on). after that try importing it again.

@Karthik Suresh 2020-01-18 17:13:42

Update numpy.

pip install numpy --upgrade

Work for me!!

@sqp_125 2019-11-22 07:51:42

So I had problems installing vtk under windows (as I use python 3.7 there is no 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 runing on a 32 bit.

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

Next I instlled that wheel:

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

Then I tested it and it worked:

python
import vtk

@user984003 2019-11-14 19:27:49

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.

@Autonomous 2015-04-24 00:18:53

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.

@sharat kanthi 2018-01-16 15:09:39

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

@Ranjan 2017-09-14 10:51:36

  1. Please make sure that you have installed python 2.7.12 or below version otherwise you will get this error definitely.
  2. Make sure Oracle client is 64 bit installed if OS is 64 Bit.
  3. Make sure Microsoft Visual C++ Compiler for Python 2.7 is 64 for bit for 64 bit Os or 32 bit for 32 bit. Note:- IF ur OS is 64 bit install all package of 64 bit or if Os is 32 bit install 32 bit package.

@Ramesh-X 2017-06-10 14:14:57

You can install opencv from official or unofficial sites.

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

@user3233042 2017-06-05 04:22:34

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 and downloaded a 64 bit Windows installer and reinstalled Python again and it worked. Hope this helps you.

@Abhishek Kashyap 2017-03-23 17:28:30

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

  1. I downloaded pywin32 Wheel file from here, then

  2. I uninstalled the pywin32 module. To uninstall execute the following command in 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 solvs the problem for me. You may also like to give it a try. Hope it work for you as well.

@MatrixManAtYrService 2017-03-15 21:54:58

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. 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 DependencyWalker and finding that it wasn't valid.

@alireza 2017-02-18 07:16:26

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

@Michael Ye 2017-01-31 13:43:52

I found the solution, maybe you can try to use the cmd window rather than the anaconda prompt window to start you first scrapy test.

@Ezekiel Kruglick 2016-08-09 00:38:55

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.

@Emad Y 2017-01-02 12:49:54

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!

@eacousineau 2017-02-08 15:58:16

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.

@Al Lelopath 2016-01-14 16:38:10

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 Project > Properties > PyDev, I had the Interpreter set to Python2.7.11.

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

@olive_tree 2014-11-30 21:08:41

I just had this problem, it turns it was just because I was using x64 version of the opencv file. Tried the x86 and it worked.

@Amalgovinus 2014-11-28 11:23:37

In my case, I have 64bit 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.

@Clive Verghese 2014-08-20 06:45:11

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.

@LarsH 2014-08-20 10:42:17

As mentioned in the question, I was using 64-bit Python.

@Kill Console 2013-09-27 03:00:49

Unofficial Windows Binaries for Python Extension Packages

you can find any python libs from here

@emeralddove 2017-09-23 08:48:23

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.

Related Questions

Sponsored Content

1 Answered Questions

How to import the openCV module to Pycharm

7 Answered Questions

[SOLVED] ImportError: DLL load failed: %1 is not a valid Win32 application

7 Answered Questions

[SOLVED] Can't import cv2; "DLL load failed"

1 Answered Questions

4 Answered Questions

[SOLVED] Import CV2: DLL load failed (Python in Windows 64bit)

3 Answered Questions

1 Answered Questions

1 Answered Questions

Install opencv in anaconda3

  • 2016-12-17 14:08:11
  • Grigoris Athanasiadis
  • 961 View
  • 1 Score
  • 1 Answer
  • Tags:   python opencv anaconda

3 Answered Questions

Sponsored Content