opencv / opencv-python

Automated CI toolchain to produce precompiled opencv-python, opencv-python-headless, opencv-contrib-python and opencv-contrib-python-headless packages.

Home Page:https://pypi.org/project/opencv-python/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

DLL load fails on Anaconda/Windows install (python3.dll)

talmo opened this issue · comments

After doing a simple pip install opencv-python or pip install opencv-contrib-python and trying to import the library, I ran into this issue:

λ python
Python 3.5.2 |Anaconda 4.2.0 (64-bit)| (default, Jul  5 2016, 11:41:13) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Anaconda3\lib\site-packages\cv2\__init__.py", line 7, in <module>
    from . import cv2
ImportError: DLL load failed: The specified module could not be found.

The readme suggests installing the Visual C++ Redistributable for Visual Studio 2015
but I already had it installed (actually had Visual Studio itself installed as well).

Checking the dependencies of the pyd binary in this package (using dumpbin which comes with Visual Studio):

λ dumpbin.exe C:\Anaconda3\Lib\site-packages\cv2\cv2.cp35-win_amd64.pyd /IMPORTS | grep dll
    python3.dll
    python35.dll
    MSVFW32.dll
    AVIFIL32.dll
    AVICAP32.dll
    KERNEL32.dll
    USER32.dll
    GDI32.dll
    ole32.dll
    OLEAUT32.dll
    COMDLG32.dll
    ADVAPI32.dll

After checking if these were in the PATH I was surprised to find that python3.dll was the only one that wasn't!

Although python3.dll comes with the standard CPython install, it's not packaged with Anaconda for some reason. ContinuumIO/anaconda-issues#1394 references this and it seems like it's just not supported(?).

As a workaround suggested in that thread, I solved this by just copying python3.dll from the official CPython 3.5.2 binaries (specifically from the Windows x86-64 embeddable zip file) into C:\Anaconda3, although I imagine it would work being anywhere in your PATH.

I thought I'd share this bit of troubleshooting since Anaconda is a popular distribution among academics and so is OpenCV, so I figure I might not be the only one running into this issue. Maybe a note in the readme might help others until Anaconda resolves this?

Alternatively, the menpo/opencv3 Anaconda package would also work but I don't think it supports ffmpeg nor opencv-contrib.

For reference, I'm running on Python 3.5.2 which came with Anaconda 4.2.0 (official installer), although conda info reports a slightly newer version:

λ conda info
Current conda install:

               platform : win-64
          conda version : 4.3.16
       conda is private : False
      conda-env version : 4.3.16
    conda-build version : 2.0.2
         python version : 3.5.2.final.0
       requests version : 2.12.4
       root environment : C:\Anaconda3  (writable)
    default environment : C:\Anaconda3
       envs directories : C:\Anaconda3\envs
                          C:\Users\Talmo\AppData\Local\conda\conda\envs
                          C:\Users\Talmo\.conda\envs
          package cache : C:\Anaconda3\pkgs
                          C:\Users\Talmo\AppData\Local\conda\conda\pkgs
           channel URLs : https://repo.continuum.io/pkgs/free/win-64
                          https://repo.continuum.io/pkgs/free/noarch
                          https://repo.continuum.io/pkgs/r/win-64
                          https://repo.continuum.io/pkgs/r/noarch
                          https://repo.continuum.io/pkgs/pro/win-64
                          https://repo.continuum.io/pkgs/pro/noarch
                          https://repo.continuum.io/pkgs/msys2/win-64
                          https://repo.continuum.io/pkgs/msys2/noarch
            config file : None
           offline mode : False
             user-agent : conda/4.3.16 requests/2.12.4 CPython/3.5.2 Windows/10 Windows/10.0.15063

Cheers and thanks for all the work you put into this repo!

Thanks, that's a really good and detailed report. I'll add a mention about the dependency to the README.

What comes to Anaconda support in general, these packages are targeted only for the original CPython. They may work with other distributions, but it's better to use for example Anaconda's package management system to install packages for that distribution.

Personally I like to use official Python versions because many of the scientific packages can be nowadays found (except SciPy's Windows builds) as precompiled wheels from PyPI. This was not true few years back but the situation is getting better. It would be nice if we had all packages available in PyPI but I guess it's not yet possible. Some of the packaging work is done currently twice (like the menpo/opencv3 package) so the situation is a bit suboptimal as the people who are willing to do this work are somewhat rare in the open source world.

I am having this same issue. I just updated the visual studio but that didn't fix it :(

Well, as @talmo wrote above, the issue is in Anaconda. The linkage rules in PEP 384 clearly state that under Windows applications are linked with python3.dll. You just have to download the correct python3.dll and place it manually to path until Anaconda provides a fix for this.

do you mean the anaconda path or the open cv path? @skvark

I think any place that is in the PATH environment variable should be enough. For example C:\Anaconda3 if that's where your installation is located.

@skvark now I get ImportError: DLL load failed: %1 is not a valid Win32 application. When Trying to import after putting the dll with anaconda

As the error says, you have probably downloaded a 64-bit version while your installation is 32-bit.

I have the 64 windows. Do you mean my python installation?

Yes, I mean Python installation. Please ask this kind of questions in the future for example in Stackoverflow or in some other QA forum.

Yea kinda got out of hand. Sorry for that. Do hope this dialogue would be helpful to at least 1 other person.

Thanks, this helped a lot!

Thanks, finally got it working!

Thank you . Awesome explanation. I was able to resolve it. @talmo

Perfect! Successfully solved my problem.

i meet the same problem, and i solve it from stackoverflow: https://stackoverflow.com/questions/43184887/dll-load-failed-error-when-importing-cv2

If you are using Anaconda, this is a problem in Anaconda release. (Refer this issue)

You can fix this issue by copying python3.dll file to Anaconda3 folder (where python.exe is located)

How to get "python3.dll"

In cmd, type python --version to find whether your installation is 64-bit or 32-bit
download python 3.x embeddable zip file from here
Extract the zip file and copy python3.dll file to Anaconda3 folder

The latest Anaconda releases include python3.dll so I'm closing this issue now. However, I will keep reference to this issue in README.

Thank you! It solves my case.

thanks,it really helps me!!

For my case, there is already python3.dll in Anaconda3 file. Still, there is the importerror for cv2.

The version of Anaconda is 64 bits, the installed python in Anaconda is 32 bits, I wonder if this kind of version mismatch cause the import error?

BTW, other packages such keras and numpy work well.

I have no idea about Anaconda or how it works internally. I presume you are using Windows? Check the FAQ in readme: https://github.com/skvark/opencv-python#frequently-asked-questions

How did you install opencv-python? Have you removed other conflicting OpenCV installations?

Also, what's the exact error? If the error is the same as in this issue, then check the .pyd file with http://www.dependencywalker.com/ to see which dependency is missing.

Thank you very much.

Yes, it's on Windows. And the method in this website solved my problem. https://www.scivision.co/install-opencv-python-windows/

Previously, I installed opencv using conda install opencv, while now I use conda install -c conda-forge opencv.

Then your issue had nothing to do with opencv-python or this repository. Please report conda issues to conda repositories in the future.

I'm using win7.
When I open cmd under Anaconda3\Lib\site-packages (where opencv is installed), I'm able to import cv2.
Other than this directory, it reports specified module could not be found.
I add the directory to the system PATH variable. But still don't work.
What do you think might be the cause?

Sounds like you're not using opencv-python (pip install opencv-python) package. This is issue tracker, not a QA forum. Please ask your question at Stack Overflow.

use conda update -all to get a stable environment. Then everything will be alright.

I install a totally new windows 7 system,and I encountered the same problem as well.Then I find the dll below in other machine ,put it into C:\Windows\System32, and I solve the import cv2 problem.
my test machine info:windows 7 ultimate;Anaconda 5.0.1.
api-ms-win-downlevel-shlwapi-l1-1-0.dll

It works well on windows 7.Thanks! @kenliang18
download api-ms-win-downlevel-shlwapi-l1-1-0.dll file and put it in 'C:\Windows\Sysytem32'
then run command regsvr32 api-ms-win-downlevel-shlwapi-l1-1-0.dll /s in cmd

@kenliang18 thanks! it worked for me too, and i just push it into C:\Windows\System32 without running command regsvr32 api-ms-win-downlevel-shlwapi-l1-1-0.dll