Python 导入 cv2 (opencv) 时出现“DLL 加载失败”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/52349669/
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
"DLL load failed" when import cv2 (opencv)
提问by Neo Anderson
In [windows server 2012 R2 x64, python 3.7 64x]
在 [windows server 2012 R2 x64, python 3.7 64x]
pip install opencv-contrib-python
installed without any error .
and when I try to import it
安装没有任何错误。
当我尝试导入它时
import cv2
show me this error :
告诉我这个错误:
Traceback (most recent call last):
File "test.py", line 1, in <module>
import cv2
File "C:\Program Files\Python37\lib\site-packages\cv2\__init__.py", line 3, in
<module>
from .cv2 import *
ImportError: DLL load failed: The specified module could not be found.
and I see another topic about this error but their solution does not work for me :
- DLL load failed error when importing cv2
- Failure to import cv2 in python : DLL load failed
- and a lot of pages in here
some of them say about that problem because anaconda, and I don't have anaconda even. and many say that problem 32 or 64 version, I try both and disappointment. many about system variables...
what dll
failed to load? cv2? why?
我看到另一个关于此错误的主题,但他们的解决方案对我不起作用:
-导入 cv2 时出现 DLL 加载失败错误
-在 python 中导入 cv2 失败:DLL 加载失败
-此处有很多页面,
其中一些页面对此进行了说明问题是因为 anaconda,我什至没有 anaconda。而且很多人说32或64版本有问题,我两个都试了,失望。许多关于系统变量...
什么dll
加载失败?简历2?为什么?
what i try till now:
我到现在为止都在尝试什么:
change python version : 3.5, 3.7, 2.7 in 86x and 64x (uninstall complete last version and delete all remain files in program files ... then install new one)
try with another packages like : opencv-python, opencv-contrib, try install specific versions with .whl (in some topic i read about this so install numpy-1.14.5+mkl-cp37-cp37m-win_amd64.whland opencv_python-3.4.3+contrib-cp37-cp37m-win_amd64.whlbut anything not change)
import numpy before cv2 for every test
installed Microsoft Visual C++ 2015 Redistributable Update 3 , 2008 , 2010 , 2017 for 64x
更改 python 版本:86x 和 64x 中的 3.5、3.7、2.7(卸载完整的最新版本并删除程序文件中的所有剩余文件......然后安装新的)
尝试使用其他软件包,例如:opencv-python、opencv-contrib,尝试使用 .whl 安装特定版本(在我读到的某些主题中,因此安装numpy-1.14.5+mkl-cp37-cp37m-win_amd64.whl和opencv_python-3.4 .3+contrib-cp37-cp37m-win_amd64.whl但没有任何改变)
在每次测试的 cv2 之前导入 numpy
已安装 Microsoft Visual C++ 2015 Redistributable Update 3、2008、2010、2017 for 64x
UPDATE
NEXT 2 DAY
下 2 天更新
I try to manage missing dll
base on dependency walkerthat mention in bellow post, download all dll
and put them in system 32, its a good clue but not solve the problem.
我尝试根据下面的文章中提到的依赖项walker来管理缺失的dll
基础,下载所有并将它们放在系统32中,这是一个很好的线索,但没有解决问题。dll
DAY 4,5
第 4,5 天
my next step , trying compile OpenCV in my machine , install visual studio 2012 + cmake and base on documentcompile 2 version , 64x and 32 , compiling have a lot of problem itself (like missing some lib from c lang and need to install Visual C++ Redistributable for Visual Studio 2012...) and I resolve all of them ,then I have 2 success compiled version but both have the same problem with missing dll
, the compile process is so slow and give 2 day from me. so if fail on this mission :/ I search for a new way...
我的下一步,尝试在我的机器上编译 OpenCV,安装 Visual Studio 2012 + cmake 并基于文档编译 2 版本,64x 和 32,编译本身有很多问题(比如从 c lang 中丢失了一些 lib 并且需要安装Visual C++适用于 Visual Studio 2012 的 Redistributable...) 并且我解决了所有问题,然后我有 2 个成功的编译版本,但两者都存在相同的丢失问题dll
,编译过程太慢,给了我 2 天的时间。所以如果这个任务失败了:/我会寻找一种新的方式......
DAY 6
第六天
my next step is to try to run OpenCV in my machine with docker ( I try but docker windows just install on windows 10 and windows server 2016) so I search for an alternative. I found Vagrant that work like docker but with VM (visual box) today my time spend for this articles, it's amazing, the concept of docker and vagrant is so Attractive, and I play with this like that I have new puzzle :) first error show on ... and limitless errors go on :
我的下一步是尝试使用 docker 在我的机器上运行 OpenCV(我尝试过,但 docker windows 只是安装在 windows 10 和 windows server 2016 上),所以我寻找替代方案。我发现 Vagrant 像 docker 一样工作,但是今天我在 VM(可视化框)上花了我的时间来写这篇文章,太棒了,docker 和 vagrant 的概念非常有吸引力,我像这样玩我有新的谜题:) 第一个错误继续显示......并且继续出现无限错误:
Day 7
第 7 天
Vagrant tests failed too because my windows server 2012 is a vb machine (visual server) and run a visual box in another visual box impossible! so I need new clues :
Vagrant 测试也失败了,因为我的 windows server 2012 是一台 vb 机器(视觉服务器)并且在另一个视觉框中运行一个视觉框是不可能的!所以我需要新的线索:
Day 8-9
第 8-9 天
try install vagrant with VMware , I download VMware, unfortunately this plugin is not free (79$) so I try to work with pythonanywhere.comthat 'gives you access to machines with a full Python environment already installed.'in this free account, you can't openurl with urllib , and more limit... so I wanna try to install ubuntu in VMware my self... I read here results after work. it's Impossible with same reason vb (visual box)
尝试使用 VMware 安装 vagrant,我下载了 VMware,不幸的是这个插件不是免费的(79 美元)所以我尝试使用pythonanywhere.com,它“让你可以访问已经安装了完整 Python 环境的机器。” 在这个免费帐户中,您不能使用 urllib 和更多限制来打开url...所以我想尝试自己在 VMware 中安装 ubuntu...我下班后在这里阅读了结果。由于同样的原因,这是不可能的 vb(可视框)
回答by anand_v.singh
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.
如果您使用的是 Windows 10 N 发行版,则可能会发生这种情况,N 发行版未预装 Windows Media 功能包,这是 OpenCV 3.4 版及更高版本所必需的。
The preferred solution is to install the feature pack at : https://www.microsoft.com/en-us/software-download/mediafeaturepack
首选解决方案是在以下位置安装功能包:https: //www.microsoft.com/en-us/software-download/mediafeaturepack
Be careful to choose the version that works with your current version of windows.
请小心选择适用于您当前 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
如果这不是一个选项,请回退到没有 dll 依赖项的较早版本的 OpenCV,您可以通过以下方式执行此操作: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.
自从 windows 推出 N 版本以来,这个问题已经在很多地方看到过,并且对 windows 环境有很多影响,识别是否有这个问题的最快方法是在 Edge 浏览器中打开 youtube,如果它说没有找到 HTML5 媒体插件, 这就是问题。
回答by Venify
I had the same error (although I compiled OpenCV myself), in my case there were some DLL dependencies missing. You can check that with the program Dependency Walker.
我有同样的错误(虽然我自己编译了 OpenCV),在我的例子中缺少一些 DLL 依赖项。您可以使用程序Dependency Walker进行检查。
Download Dependency Walker and run it, and open the file cv2.pydwith Dependency Walker, it should be in C:\Program Files\Python37\lib\site-packages\cv2\
if the pip package installed correctly.
下载Dependency Walker并运行,用Dependency Walker打开文件cv2.pyd,C:\Program Files\Python37\lib\site-packages\cv2\
如果pip包安装正确,应该就在里面。
This program then scans for dependencies (which may take a few minutes) and it then shows missing DLL files in red.
该程序然后扫描依赖项(这可能需要几分钟),然后以红色显示丢失的 DLL 文件。
回答by Vikhyat Agarwal
Python 3.7 may not support some modules.
Python 3.7 可能不支持某些模块。
- Try installing python 3.6.4
- Using command prompt, "pip install opencv" or "pip install opencv-python"
- Import cv2
- 尝试安装 python 3.6.4
- 使用命令提示符,“pip install opencv”或“pip install opencv-python”
- 导入cv2
回答by Omda asli
I had similar issue except that I'm using Anaconda3 and this code here solved it:
我有类似的问题,除了我使用的是 Anaconda3 并且这里的代码解决了它:
conda install -c anaconda py-OpenCV
回答by Biks
I have faced the similar issue in Windows Server 2012 r2. After lot of findings I found that mfplat.dll was missing which is related to Window Media Service.
我在 Windows Server 2012 r2 中遇到过类似的问题。经过大量调查后,我发现缺少与 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。
- Turn windows features on or off
- Skip the roles screen and directly go to Feature screen
- Select "Desktop Experience" under "User Interfaces and Infrastructure"
- 打开或关闭 Windows 功能
- 跳过角色界面,直接进入功能界面
- 在“用户界面和基础设施”下选择“桌面体验”
After this all required dll of opencv would be available.
在此之后,所有需要的 opencv dll 都将可用。
回答by Rakshith G B
Can you try uninstalling opencv and installing it using a wheel file instead? Hereis a website that has many versions of OpenCV compiled for windows, search for the one you need and simply install it with pip
command.
您可以尝试卸载opencv并使用wheel文件安装它吗?这是一个为 Windows 编译了许多版本的 OpenCV 的网站,搜索您需要的版本并使用pip
命令简单地安装它。
So if you have Python 3.6 (64 Bit) and wish to install OpenCV 3.4.3 then download the wheel file:
opencv_python?3.4.3?cp36?cp36m?win_amd64.whl
因此,如果您有 Python 3.6(64 位)并希望安装 OpenCV 3.4.3,请下载轮文件:
opencv_python?3.4.3?cp36?cp36m?win_amd64.whl
回答by Nell
After 15 days of brain storming, This solution worked for me. And I am sure that it will work for you too. I installed anaconda to use OpenCV 3.1.0. I followed following Steps:
经过 15 天的头脑风暴,这个解决方案对我有用。我相信它对你也有用。我安装了 anaconda 以使用 OpenCV 3.1.0。我遵循以下步骤:
1) I have installed anaconda-5.3 64-bit installer (614.3 MB) which uses python 3.7. You can download anaconda from link: https://www.anaconda.com/download/#windows
1) 我已经安装了使用 python 3.7 的 anaconda-5.3 64 位安装程序 (614.3 MB)。您可以从链接下载 anaconda:https: //www.anaconda.com/download/#windows
2) After installation of anaconda, open anaconda prompt by typing "anaconda prompt" on windows start button. Open with "Run as administrator".
2)安装anaconda后,在windows开始按钮上输入“anaconda prompt”打开anaconda prompt。以“以管理员身份运行”打开。
3) You have to create new environment to install and use OpenCV module.Write following commands to create and activate new environment:>conda create --name myNewEnv python=3.5.0
>activate myNewEnv
"myNewEnv" is the name of new enviroment.
3)你必须创建新环境来安装和使用OpenCV模块。编写以下命令来创建和激活新环境:>conda create --name myNewEnv python=3.5.0
>activate myNewEnv
“myNewEnv”是新环境的名称。
4)Now you need to install prerequisite for OpenCV, which is numpy and then install opencv3. Start internet before executing following commands:>conda install numpy
>conda install anaconda-client
>conda install --channel https://conda.anaconda.org/menpo opencv3
4)现在你需要安装OpenCV的先决条件,即numpy,然后安装opencv3。在执行以下命令之前启动互联网:>conda install numpy
>conda install anaconda-client
>conda install --channel https://conda.anaconda.org/menpo opencv3
5)OpenCV3 has been installed. Now, verify installation by executing following commands:(myNewEnv) C:\Users\Nilesh> python
>>>import cv2
>>>cv2.__version__
5)OpenCV3 已经安装。现在,通过执行以下命令来验证安装:(myNewEnv) C:\Users\Nilesh> python
>>>import cv2
>>>cv2.__version__
Follow steps given below, when you want to start OpenCV3 second time onwards:
当您想第二次启动 OpenCV3 时,请按照以下步骤操作:
1) Open "anaconda command promt" from start menu with "run as administrator" rights
2) Type command >activate your_new_environment_name
3) Assume that you want to run file located on desktop, for that write following command:> cd C:\Users\Nilesh\Desktop
4) Now your working directory is Desktop.
5) Type following command to run any code (for example test.py).> python test.py
Note: Here, python means version-3.
1) 使用“以管理员身份运行”权限从开始菜单打开“anaconda command promt”
2) 输入 command >activate your_new_environment_name
3) 假设您要运行位于桌面上的文件,为此编写以下命令:> cd C:\Users\Nilesh\Desktop
4) 现在您的工作目录是桌面。
5) 键入以下命令以运行任何代码(例如 test.py)。> python test.py
注意:这里,python 表示版本 3。
I hope this solution will work for you.
我希望这个解决方案对你有用。
回答by luka_matkic
I had the same problem on Windows Server 2012 R2 x64. I was creating executable file using PyInstaller and got error in runtime:
我在 Windows Server 2012 R2 x64 上遇到了同样的问题。我正在使用 PyInstaller 创建可执行文件并在运行时出错:
ImportError: DLL load failed: The specified module could not be found.
After installing "Visual C++ redistributable" 2015 and enabling "Media Foundation" feature my problem was resolved.
安装“Visual C++ redistributable”2015 并启用“Media Foundation”功能后,我的问题得到解决。
There is more informations in documentation: OpenCV Documentation
文档中有更多信息:OpenCV Documentation
回答by Faizi
I was having the same issue. I resolved this error by downgrading open cv.
我遇到了同样的问题。我通过降级 open cv 解决了这个错误。
pip install opencv-python==3.3.0.9
pip install opencv-python==3.3.0.9
回答by Rhythm sharma
i was suffering from the same problem "DLL load failed" after reading tons of answers and articles i got a solution.
在阅读了大量答案和文章后,我遇到了同样的问题“DLL 加载失败”,我得到了解决方案。
i don't know this works for you or not but give it a try.
我不知道这对你有用与否,但试一试。
tools and versions i used: anaconda - 5.3.1, python - 3.7, win 10 (64 bit)
我使用的工具和版本:anaconda - 5.3.1,python - 3.7,win 10(64 位)
Steps i performed :
我执行的步骤:
step1:i installed opencv 3.4.4 from herethen extract into C drive (you can do wherever you want)
第1步:我从这里安装了opencv 3.4.4 然后解压到C盘(你可以随心所欲)
step2: copy cv2.pyd file from [C:\opencv\build\python\cv2\python-3.7] here to [C:\Users\"user-name"\Anaconda3\Lib\site-packages] here.
step2:将 cv2.pyd 文件从 [C:\opencv\build\python\cv2\python-3.7] 这里复制到 [C:\Users\"user-name"\Anaconda3\Lib\site-packages] 这里。
step3: run dependency walkerto detect which dllfiles are missing, after running dependency walker in my case it showed two dll files are not loading, they are (1)OPENCV_WORLD344.DLL and (2)IESHIMS.DLL. i don't know about your situation but you can do this-> find files name using dependency walker then find their path and follow step4.
step3:运行dependency walker来检测哪些dll文件丢失,在我的例子中运行dependency walker后它显示两个dll文件没有加载,它们是(1)OPENCV_WORLD344.DLL和(2)IESHIMS.DLL。我不知道您的情况,但您可以执行此操作-> 使用依赖项walker 查找文件名,然后找到它们的路径并按照第 4 步操作。
step4: open System property->Advanced->Environment variables, now edit path and add C:\opencv\build\x64\vc14\bin (in my case this was the place where OPENCV_WORLD344.DLL is stored) and after doing this, import cv2 in python.
step4:打开系统属性->高级->环境变量,现在编辑路径并添加C:\opencv\build\x64\vc14\bin(在我的例子中这是存储OPENCV_WORLD344.DLL的地方),然后,在python中导入cv2。
If my answer does not work then add a comment.
如果我的回答不起作用,请添加评论。