Python 无法导入cv2;“DLL 加载失败”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33200025/
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 13:00:06  来源:igfitidea点击:

Can't import cv2; "DLL load failed"

pythonwindowsopencvnumpywindows-10

提问by Luke Vincent

I really didn't want to start my own question with this because it seems to be a common error here. However, having wasted hours on this now and having followed every thread I could find, none of the given answers have sorted this for me.

我真的不想用这个开始我自己的问题,因为这似乎是这里的一个常见错误。然而,现在已经在这上面浪费了几个小时并跟踪了我能找到的每个线程,没有一个给定的答案为我解决了这个问题。

So my only option is to provide all the information I can about my set up and hope one of you lot can recognise the problem.

所以我唯一的选择是提供我所能提供的关于我的设置的所有信息,并希望你们中的一个人能够认识到这个问题。

I'm running on windows 10(x64) and have installed the following prebuilt binaries from the collection here.

我在 Windows 10(x64) 上运行,并已从此处的集合中安装了以下预构建的二进制文件。

  1. python 3.5.0-win32 (installed from python website)
  2. numpy 1.10-cp35-win32
  3. matplotlib 1.4.3-cp35-win32
  4. opencv 3.0.0-cp35-win32
  1. python 3.5.0-win32(从python网站安装)
  2. numpy 1.10-cp35-win32
  3. matplotlib 1.4.3-cp35-win32
  4. opencv 3.0.0-cp35-win32

Everything was installed using "pip install file_name.whl" with no error messages. However the difference is open cv will not import and I get the following error:

一切都是使用“pip install file_name.whl”安装的,没有错误消息。但是,不同之处在于 open cv 不会导入,并且出现以下错误:

>>> import cv2
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 4.5.4\helpers\pydev\pydev_import_hook.py", line 21, in do_import
    module = self._system_import(name, *args, **kwargs)
ImportError: DLL load failed: The specified module could not be found.

cv2.cp35-win32.pydplus many .dllfiles are installed to C:\Python35\Lib\site-packages

cv2.cp35-win32.pyd加上很多.dll文件安装到C:\Python35\Lib\site-packages

I've tried renaming cv2.cp35-win32.pydto cv2.pydand copying the .dllfiles to C:\Python35\DLLs

我尝试将cv2.cp35-win32.pyd重命名为cv2.pyd并将.dll文件复制到C:\Python35\DLLs

I've tried using another python opencv package downloaded from the site, adding the bin directory to my PATH environment and moving the cv2.pydfile to C:\Python35\Lib\site-packages

我尝试使用从站点下载的另一个 python opencv 包,将 bin 目录添加到我的 PATH 环境并将cv2.pyd文件移动到C:\Python35\Lib\site-packages

The same error appears every time. Any help would be appreciated!

每次都会出现同样的错误。任何帮助,将不胜感激!

采纳答案by Brian Ng

In my situation, when using Pycharm to import cv2, it returned ImportError: DLL not found. However, using python intepreter to import I got *ImportError: ... not a win32 DLL ... *instead. So, in this situation, I had to download Visual C++ 2015 redistribution package and problem solved.

在我的情况下,当使用 Pycharm 导入 cv2 时,它返回ImportError: DLL not found。但是,使用 python 解释器导入我得到了*ImportError: ... not a win32 DLL ... *相反。因此,在这种情况下,我不得不下载 Visual C++ 2015 重新分发包并解决了问题。

So to anyone who meets the same error, please make sure you have the VC++ redistribution package which was used to compile the DLL packed along the module.

因此,对于遇到相同错误的任何人,请确保您拥有用于编译随模块打包的 DLL 的 VC++ 重新分发包。

回答by Luke Vincent

Ok so I've got it working by completely uninstalling everything and starting from the beginning with Python 2.7.10.

好的,所以我通过完全卸载所有内容并从 Python 2.7.10 开始,使其正常工作。

It simply just does not work with Python 3. For clarity on what I did I followed these two videos in the order I'm posting them:

它只是不适用于 Python 3。为了清楚我做了什么,我按照发布它们的顺序关注了这两个视频:

How to install opencv + dependencies

如何安装opencv + 依赖项

How to set up the path variable

如何设置路径变量

回答by ucancekirge

I managed to solve the problem with uninstalling all Visual C++ Redistributable Packages until 2008 and then installing Visual C++ Redistributable Packages for VS 2015. The explanation is here: http://answers.opencv.org/question/74384/opencv-python-30-with-python-35-on-windows/

我设法解决了卸载所有 Visual C++ Redistributable Packages 直到 2008 然后安装 Visual C++ Redistributable Packages for VS 2015 的问题。解释在这里:http: //answers.opencv.org/question/74384/opencv-python-30 -with-python-35-on-windows/

回答by Jumabek Alikhanov

I solved it by Adding path to opencv DLLs to PATH environment variable

我通过将 opencv DLL 的路径添加到 PATH 环境变量来解决它

You can follow thisguide on how to add path to environment variable

您可以按照指南了解如何将路径添加到环境变量

回答by RedPanda

I had the error with Python 3.4 and opencv-python 3.3 on Windows 10. In my case the problem was due to my Anaconda distribution. I solved it with the solution from the opencv-python project page, which points to this github issue.

我在 Windows 10 上使用 Python 3.4 和 opencv-python 3.3 时出现错误。在我的情况下,问题是由于我的 Anaconda 发行版。我用 opencv-python项目页面中的解决方案解决了这个问题,该页面指向了这个github 问题

The problem is a missing python3.dllin the anaconda distribution. You can download the python binaries hereand extract dll out of the zip archive. Put it in a folder in your PATH (e.g. C:\Users\MyName\Anaconda3) and the import should work.

问题出python3.dll在 anaconda 发行版中。您可以在此处下载 python 二进制文件并从 zip 存档中提取 dll。将它放在 PATH 中的一个文件夹中(例如 C:\Users\MyName\Anaconda3),导入应该可以工作。

回答by Ari

This worked for me:

这对我有用:

pip install opencv-contrib-python

pip install opencv-contrib-python

(Running Python 3.6.3 - 32bit, on Windows 7 - 64bit)

(运行 Python 3.6.3 - 32 位,在 Windows 7 - 64 位)

回答by Ismael EL ATIFI

I had the same issue with Windows 10 Professionnal N and solved it just by installing the Media Feature Pack (and restarting windows after installation).

我在 Windows 10 Professionnal N 上遇到了同样的问题,只需安装 Media Feature Pack(并在安装后重新启动 Windows)即可解决。

Windows N and KN editions do not include Media Feature Pack which is required by OpenCV. If you are using Windows N or KN edition, please install also Windows Media Feature Pack.

Windows N 和 KN 版本不包括 OpenCV 所需的媒体功能包。如果您使用的是 Windows N 或 KN 版本,请同时安装Windows Media Feature Pack

From https://github.com/skvark/opencv-python

来自https://github.com/skvark/opencv-python