>问题复现
1.opencv-python的安装
利用pip可以很简单的完成安装:
pip install opencv-python
(我已在此之前安装了numpy1.14.2+mkl)
2.ImportError: DLL load failed:找不到指定模块
我遇到问题的这台机子的IDE环境是PyCharm+Windows(64bit),在正确配置Python Interpreter的情况下(最好手动指定,而不是默认使用工程中的venv/python.exe),依旧报出:ImportError: DLL load failed: The specified module could not be found.
的错误。
>问题的解决
在PyPI的opencv页面,有以下QA ——
Q: Import fails on Windows: ImportError: DLL load failed: The specified module could not be found.?
A: -If the import fails on Windows, make sure you have Visual C++ redistributable 2015 installed.
-If you are using older Windows version than Windows 10 and latest system updates are not installed, Universal C Runtime might be also required.
-If the above does not help, check if you are using Anaconda. Old Anaconda versions have a bug which causes the error, see this issue for a manual fix.
这个QA主要说明了几种常见的解决方案,在末尾的github issue中,也可以看到有关Anaconda
安装opencv-python的解决方案,在某种情况下已经足够应对大部分问题了。
但是, 在确保我安装了正确位数的python和对应的opencv-python以及Visual C++ redistributable 2015
的情况下,我依旧不能正常运行opencv-python。
但是我在那篇Issue中找到了有趣的解决方案——利用dll依赖追踪器找到缺失的DLL,并手动补上:比如我用的是这款软件——depends。使用方法也很简单,就是找到cv2.pyd(这个东西可以直接在python的安装目录直接ctrl+f
搜索到,或者用cmd键入:pip show opencv-python
,到Location
指向的地址中找到文件夹cv2
),拖到depends软件的界面中即可:
可以看到我缺失了两个DLL(虽然不知道为什么缺失了这两个dll):
- python27.dll
- API-MS-WIN-DOWNLEVEL-SHLWAPI-L1-1-0.DLL
第一个DLL可以在C:\Windows\System32
中找到,复制到…/cv2文件夹中即可,第二个可以到动态链接库网站下载,同样放到…./cv2文件中即可(或者放到./python/Lib
文件夹中,我个人比较推荐放到cv2
文件夹中,比较不容易弄混这些dll的依赖关系)。
>测试opencv-python
# coding:utf-8
import cv2
image = cv2.imread(./'a.jpg')
cv2.imshow('show',image)
cv2.waitKey(0)
弹出界面: