1
votes

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

Does anyone know how to fix this? This problem occurs when i am trying to import cv2. My laptop is 64bit and installed 64bit python, i also put the cv2.pyd file in the site-packages folder of Python.

My PYTHONPATH value = C:\Python35;C:\Python35\DLLs;C:\Python35\Lib;C:\Python35\libs;C:\Users\CV\OpenCV\opencv\build\python\2.7\x64;%OPENCV_DIR%\bin;

My OPENCV_DIR value = C:\Users\CV\OpenCV\opencv\build\x64\vc12

I also put reference of my pythonpath and my opencv_dir to the PATH by putting **%PYTHONPATH%;%PYTHONPATH%\Scripts\;%OPENCV_DIR%;**

I also installed opencv_python-3.0.0+contrib-cp35-none-win_amd64 through pip install and command line.

None of this solved my problem.

4
Is your "python path" in reference to the system PATH or PYTHONPATH? Some of those directories (e.g. DLLs, Lib, libs) should be in neither of those environment variables.Eryk Sun
@eryksun i edited my question and yes it is referenced in PATHxzbxxzxzmn
Do not include C:\Python35;C:\Python35\DLLs;C:\Python35\Lib;C:\Python35\libs in your PYTHONPATH. It also shouldn't have any directories with implementation-specific modules such as C:\Users\CV\OpenCV\opencv\build\python\2.7\x64 -- since PYTHONPATH isn't versioned. I also doubt that %OPENCV_DIR%\bin contains Python modules. It may be necessary for it to be on the system PATH for finding DLLs.Eryk Sun
If you install Gohlke's build, it includes all of the required DLLs beside cv2.cp35-win_amd64.pyd, so you shouldn't have to do anything else. Remove everything else that you've done since it will only get in the way and cause problems if Python finds the wrong version of cv2 on sys.path. Start from a fresh install of Python if you have to, and clean up the system PATH to remove all references to OpenCV directories.Eryk Sun
Ah, I see that Gohlke's cv2.cp35-win_amd64.pyd depends on MSVCP140.dll, the C++ runtime library. Was that the DLL you need? The installer for Python 3.5 doesn't include this DLL, so in that case you probably had to install the VC++ 2015 redistributable package.Eryk Sun

4 Answers

2
votes

A bit more elaborate answer for people coming here (like me) in the future:

  1. Uninstall opencv-python, e.g. via pip uninstall opencv-python
  2. Uninstall all visual studio c++ packages
  3. Download the new visual studio from microsoft and install
  4. Download your matching python whl from gohlke and install using e.g. pip install opencv-python-3.2.0+contrib
  5. Now it should work...
1
votes

(Posting @eryksun comment as a community answer to prevent system from autodeleting question)

Gohlke's cv2.cp35-win_amd64.pyd depends on MSVCP140.dll, the C++ runtime library.

Was that the DLL you need?

The installer for Python 3.5 doesn't include this DLL, so in that case you probably had to install the VC++ 2015 redistributable package.

0
votes

This can happen if you are using windows 10 N distribution, the N distributions does not come pre installed with windows media feature pack, which is required after OpenCV version 3.4 and onwards.

The preferred solution is to install the feature pack at : https://www.microsoft.com/en-us/software-download/mediafeaturepack

Be careful to choose the version that works with your current version of windows.

If that is not an option, fall back to an earlier version of OpenCV that does not have dll dependencies, you can do that by:pip install opencv-python=3.3.0.9

Since windows rolled out it's N version this problem has been seen at many places, and has many impacts across the windows environment, the fastest way to identify if you have this problem is open youtube in Edge browser, if it says HTML5 media plugin not found, this is the problem.

If the problem still persists, Install dependency walker and find out which modules are causing dependency breaks.

-1
votes

in this case, I just copy file 'python3.dll' from my python3 installation folder to my virtualenv lib folder, and then it works.