Python 导入 cv2 时 DLL 加载失败错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43184887/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 22:36:39  来源:igfitidea点击:

DLL load failed error when importing cv2

pythonopencvcv2

提问by Cameron Wasilewsky

I have installed opencv on my windows machine using python 3.6 without any issues, using:

我已经使用 python 3.6 在我的 Windows 机器上安装了 opencv,没有任何问题,使用:

pip install opencv-python

but when I try to import cv2 I get the following error

但是当我尝试导入 cv2 时出现以下错误

ImportError: DLL load failed: The specified module could not be found.

I have seen this post

我看过这个帖子

It says cv2 doesn't work with python 3 I was wondering if this has been fixed or if there is a way around it

它说 cv2 不适用于 python 3 我想知道这是否已修复或者是否有解决方法

采纳答案by thewaywewere

You can download the latest OpenCV 3.2.0 for Python 3.6 on Windows 32-bit or 64-bit machine, look for file starts withopencv_python?3.2.0?cp36?cp36m, from this unofficial site. Then type below command to install it:

您可以在 Windows 32 位或 64 位机器上下载适用于 Python 3.6 的最新 OpenCV 3.2.0 opencv_python?3.2.0?cp36?cp36m,从这个非官方站点查找以 , 开头的文件。然后输入以下命令进行安装:

  • pip install opencv_python?3.2.0?cp36?cp36m?win32.whl(32-bit version)
  • pip install opencv_python?3.2.0?cp36?cp36m?win_amd64.whl(64-bit version)
  • pip install opencv_python?3.2.0?cp36?cp36m?win32.whl(32 位版本)
  • pip install opencv_python?3.2.0?cp36?cp36m?win_amd64.whl(64 位版本)

I think it would be easier.

我认为这会更容易。

Update on 2017-09-15:

2017-09-15 更新:

OpenCV 3.3.0wheel files are now available in the unofficial siteand replaced OpenCV 3.2.0.

OpenCV 3.3.0车轮文件现在可以在非官方网站上找到并替换为OpenCV 3.2.0.

Update on 2018-02-15:

2018-02-15 更新:

OpenCV 3.4.0wheel files are now available in the unofficial siteand replaced OpenCV 3.3.0.

OpenCV 3.4.0车轮文件现在可以在非官方网站上找到并替换为OpenCV 3.3.0.

Update on 2018-06-19:

2018-06-19 更新:

OpenCV 3.4.1wheel files are now available in the unofficial sitewith CPython 3.5/3.6/3.7support, and replaced OpenCV 3.4.0.

OpenCV 3.4.1车轮文件现在可以在非官方网站上获得CPython 3.5/3.6/3.7支持,并替换了OpenCV 3.4.0.

Update on 2018-10-03:

2018-10-03 更新:

OpenCV 3.4.3wheel files are now available in the unofficial sitewith CPython 3.5/3.6/3.7support, and replaced OpenCV 3.4.1.

OpenCV 3.4.3车轮文件现在可以在非官方网站上获得CPython 3.5/3.6/3.7支持,并替换了OpenCV 3.4.1.

Update on 2019-01-30:

2019-01-30 更新:

OpenCV 4.0.1wheel files are now available in the unofficial sitewith CPython 3.5/3.6/3.7support.

OpenCV 4.0.1车轮文件现在可以在支持的非官方网站上获得CPython 3.5/3.6/3.7

Update on 2019-06-10:

2019-06-10 更新:

OpenCV 3.4.6and OpenCV 4.1.0wheel files are now available in the unofficial sitewith CPython 3.5/3.6/3.7support.

OpenCV 3.4.6OpenCV 4.1.0车轮文件现在可以在支持的非官方网站上获得CPython 3.5/3.6/3.7

回答by tientuyen07

I took a lot of time to solve this error! Run command

我花了很多时间来解决这个错误!运行命令

pip install opencv-contrib-python

回答by Ramesh-X

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

如果您将 Anaconda 与 一起使用python 3.5,则这是 Anaconda 版本中的一个问题。(参考这个问题

You can fix this issue by copying python3.dllfile to Anaconda3folder (where python.exeis located)

您可以通过将python3.dll文件复制到Anaconda3文件夹(所在python.exe位置)来解决此问题

How to get "python3.dll"

如何获取“python3.dll”

  • In cmd, type python --versionto 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.dllfile to Anaconda3folder
  • 在 中cmd,键入python --version以查看您的安装是 64 位还是 32 位
  • 这里下载 python 3.x 可嵌入 zip 文件
  • 解压 zip 文件并将python3.dll文件复制到Anaconda3文件夹

But if you can move to Anacondawith python 3.6you will not face this issue. If it is possible for you, then it is the recommended way..

但如果你可以移动到Anacondapython 3.6你不会面临这个问题。如果您有可能,那么这是推荐的方式。

回答by Biks

Recently I have faced the similar issue in Azure Windows Server 2012 r2. Tried all option with and without Anaconda but none of them helped. After lot of findings I found that mfplat.dllwas missing which is related to Window Media Service.

最近我在 Azure Windows Server 2012 r2 中遇到了类似的问题。尝试了有和没有 Anaconda 的所有选项,但没有一个有帮助。经过大量调查后,我发现缺少与 Window Media Service 相关的mfplat.dll

Hence you have to manually install the features so that you can get dll related to window media service.

因此,您必须手动安装这些功能,以便您可以获取与窗口媒体服务相关的 dll。

1.Turn windows features on or off

1.打开或关闭windows功能

2.Skip the roles screen and directly go to Feature screen

2.跳过角色界面,直接进入功能界面

3.Select "Desktop Experience" under "User Interfaces and Infrastructure"

3.在“用户界面和基础设施”下选择“桌面体验”

After this all required dll of media services for opencv would be available.

在此之后,opencv 所需的所有媒体服务 dll 都将可用。

So if you are planning to run your code in cloud(Window Server) then please dont forget to select Desktop Experience feature.

因此,如果您计划在云(Window Server)中运行您的代码,请不要忘记选择桌面体验功能。

回答by Premil

I ran into this problem on Windows 10 (N)with a new Anaconda installation based on Python 3.7 (OpenCV version 4.0). None of the above advice helped (such as installing OpenCV from the unofficial site nor installing VC Redistributable).

我在Windows 10 (N) 上使用基于 Python 3.7(OpenCV 版本 4.0)的新 Anaconda 安装时遇到了这个问题。上述建议都没有帮助(例如从非官方站点安装 OpenCV 或安装 VC Redistributable)。

I checked DLL dependencies of ...\AppData\Local\conda\conda\envs\foo\Lib\site-packages\cv2\cv2.cp37-win_amd64.pydusing dumpbin.exeaccording to this github issue. I noticed a library MF.dll, which I figured out belongs to Windows Media Foundation.

我根据this github issue检查了...\AppData\Local\conda\conda\envs\foo\Lib\site-packages\cv2\cv2.cp37-win_amd64.pyd使用的DLL依赖项。我注意到一个 library ,我发现它属于 Windows Media Foundation。dumpbin.exeMF.dll

So I installedMedia Feature Packfor N versions of Windows 10and voilà, the issue was resolved!

所以我为 N 版本的 Windows 10安装了媒体功能包,瞧,问题解决了!

回答by Shital Shah

After spending too much time on this issue and trying out all different answers, here is what found:

在这个问题上花费了太多时间并尝试了所有不同的答案后,结果如下:

  • The accepted answer by @thewaywewere is no longer applicable. I think this was applicable when opencv-python module still wasn't available.

  • This is indeed a bug in Anaconda 4.2 because they forgot to ship python3.dll. They have fixed this in next releases but unfortunately 4.2 is the last release with Python 3.5 as default. If you are stuck with Python 3.5 (for example VS2015 is only aware of versions up to 3.5) then you must manually download python3.dll as described in answer by @Ramesh-X.

  • If you can move on to Python 3.6 (which at present bit difficult if you are using OpenCV and VS2015) then just install latest Anaconda version and you don't have to deal with any of these.

  • @thewaywewere 接受的答案不再适用。我认为这在 opencv-python 模块仍然不可用时适用。

  • 这确实是 Anaconda 4.2 中的一个错误,因为他们忘记提供 python3.dll。他们在下一个版本中修复了这个问题,但不幸的是 4.2 是最后一个默认使用 Python 3.5 的版本。如果您坚持使用 Python 3.5(例如 VS2015 只知道最高 3.5 的版本),那么您必须按照@Ramesh-X 的回答中的描述手动下载 python3.dll。

  • 如果您可以继续使用 Python 3.6(如果您使用 OpenCV 和 VS2015,目前这有点困难),那么只需安装最新的 Anaconda 版本,您就不必处理任何这些。

回答by Valentin Heinitz

There are many questions on that and many suggestions. None of them helped me for the recent Opencv 3.4.16 and Python 3.6/3.7. Finally I switched to Pyhon 2.7.15 and installed opencv 3.1.0. The DLL-problem was solved.

对此有很多问题和很多建议。他们都没有帮助我最近的 Opencv 3.4.16 和 Python 3.6/3.7。最后我切换到 Pyhon 2.7.15 并安装了 opencv 3.1.0。DLL 问题已解决。

When I look in cv2.pyd with dependency walker, the 3.1 has no dependency to one missing dll. Opencv 3.4 hasthis missing dependency to this dll:

当我使用依赖遍历器查看 cv2.pyd 时,3.1 不依赖于一个丢失的 dll。OpenCV的3.4这个丢失的依赖于这个DLL:

API-MS-WIN-DOWNLEVEL-SHLWAPI-L1-1-0.DLL

may be this is the problem.

可能这就是问题所在。

P.S.: I have Win7 pofessional 64Bit, 32Bit Python 2.7.15

PS:我有 Win7 专业版 64Bit、32Bit Python 2.7.15

回答by eric

Frankly there are a lot of very smart and complicated answers here. Mine is dumb and simple. I deleted my conda environment, re-installed from scratch, taking pains to install opencv first. This fixed my problems. Environments are meant to be temporary and diaphanous -- don't get too attached.

坦率地说,这里有很多非常聪明和复杂的答案。我的既愚蠢又简单。我删除了我的conda环境,从头开始重新安装,煞费苦心地先安装opencv。这解决了我的问题。环境是暂时的和透明的——不要太执着。

So if my environment was called fubarfirst make sure every instance is deactivated (including any IDEs that are using it). Then remove it:

因此,如果fubar首先调用我的环境,请确保每个实例都已停用(包括任何正在使用它的 IDE)。然后删除它:

conda remove --name fubar --all

Now I simply recreate my environment and add opencv first:

现在我只需重新创建我的环境并首先添加 opencv:

conda create --name fubar
conda activate fubar
conda install opencv

And then go from there. First open python and make sure import cv2works. Then you should be on your way. Note I always (always) install Spyder last as it tends to screw things up when I don't.

然后从那里走。首先打开python并确保import cv2有效。那你应该上路了。注意我总是(总是)最后安装Spyder,因为当我不这样做时它往往会把事情搞砸。

Note, if that doesn't work, we have had some cases where people have to uninstall and reinstall Anaconda, and then things worked. Obviously a last resort.

请注意,如果这不起作用,我们在某些情况下必须卸载并重新安装 Anaconda,然后一切正常。显然是最后的手段。

回答by mhaghighat

The issue is due to the missing python3.dllfile in Anaconda3.

问题是由于python3.dllAnaconda3 中缺少文件。

To fix the issue, you should simply copy the python3.dllto C:\Program Files\Anaconda3(or wherever your Anaconda3is installed).

要解决此问题,您只需将 复制python3.dllC:\Program Files\Anaconda3(或安装Anaconda3 的任何位置)。

You can get the python3.dllby downloading the binaries provided at the bottom of the Python's Release pageand extracting the python3.dllfrom the ZIP file.

您可以python3.dll通过下载Python 发布页面底部提供的二进制文件并python3.dll从 ZIP 文件中提取来获取。

回答by Kiran Kumar

I had the same issue with python 3.6(Anaconda3) and OpenCV 3.4.1 for spyder to work. Even after copying cv2.pyd into Anaconda3 Users/home/Anaconda3/Lib/site-packages, it didn't work.

我在 python 3.6(Anaconda3) 和 OpenCV 3.4.1 上遇到了同样的问题,以便 spyder 工作。即使将 cv2.pyd 复制到 Anaconda3 Users/home/Anaconda3/Lib/site-packages 后,它也不起作用。

But found a solution

但是找到了解决办法

Later I installed OpenCV and Dlib on Anaconda and copied the generated cv2.cp36-win_amd64.pyd and dlib.cp36-win_amd64.pyd into Anaconda3 Users/home/Anaconda3/Lib/site-packages. These can be copied from environment folder C:\Users\home\Anaconda3\envs\opencv\Lib\site-packages.

后来我在Anaconda上安装了OpenCV和Dlib,将生成的cv2.cp36-win_amd64.pyd和dlib.cp36-win_amd64.pyd复制到Anaconda3 Users/home/Anaconda3/Lib/site-packages中。这些可以从环境文件夹 C:\Users\home\Anaconda3\envs\opencv\Lib\site-packages 复制。

Finally spyder started to work

终于spyder开始工作了