有效解决Anaconda下视觉捕捉opencv报错“DLL load failed while importing cv2”

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

前言

尝试对机器视觉学习,在Anaconda3下导入opencv模块,电脑接USB视像头,出现opencv报错“DLL load failed while importing cv2“错误,网上关于这个错误的解决办法太多,尝试,折腾好久都不行,最后发现关键时python和spyder版本不兼容导致的,以供大家少走弯路!

1.源代码

代码如下(直接用网上的):

import cv2
if __name__ == '__main__':
    # 捕捉帧,笔记本摄像头设置为0即可
    capture = cv2.VideoCapture(0)
  
    while (True):
        ret, frame = capture.read()
               cv2.imshow('frame', frame)
        if cv2.waitKey(1) == ord('q'):  # 按Q退出
            break

2.不断尝试,发现根源

1.尝试重新多种方法安装opencv,失败
2.尝试在VScode可以成功调用摄像头!
3.尝试新建环境,安装opencv后,安装spyder失败,从提示信息中发现问题所在,后来网上也有类似帖子!大神无处不在啊
The following packages are incompatible
├─ pin on python 3.13.* =* * is installable and it requires
│ └─ python =3.13 , which can be installed;
└─ spyder =
* is not installable because there are no viable options
├─ spyder [5.1.5|5.2.2|…|6.0.4] would require
│ └─ python >=3.10,❤️.11.0a0 *, which conflicts with any installable versions previously reported;
├─ spyder [5.4.1|5.4.2|…|6.0.4] would require
│ └─ python >=3.11,❤️.12.0a0 *, which conflicts with any installable versions previously reported;
├─ spyder [3.3.6|4.0.1|…|6.0.1] would require
│ └─ python >=3.8,❤️.9.0a0 *, which conflicts with any installable versions previously reported;
├─ spyder [4.2.0|4.2.1|…|6.0.4] would require
│ └─ python >=3.9,❤️.10.0a0 *, which conflicts with any installable versions previously reported;
├─ spyder [5.5.0|5.5.1|…|6.0.4] would require
│ └─ python >=3.12,❤️.13.0a0 *, which conflicts with any installable versions previously reported;
├─ spyder [2.3.9|3.1.3|…|4.1.1] would require
│ └─ python [=2.7 *|>=2.7,<2.8.0a0 *], which conflicts with any installable versions previously reported;
├─ spyder [2.3.9|3.1.2|3.1.3] would require
│ └─ pyqt =4.11 *, which requires
│ └─ qt =4.8 *, which does not exist (perhaps a missing channel);
├─ spyder 2.3.9 would require
│ └─ python =3.4 * but there are no viable options
│ ├─ python 3.4.5 would require
│ │ └─ vc =10 , which does not exist (perhaps a missing channel);
│ └─ python 3.4.5 would require
│ └─ vs2010_runtime =
*, which does not exist (perhaps a missing channel);
├─ spyder [2.3.9|3.1.3|…|3.3.1] would require
│ └─ python [=3.5 *|>=3.5,❤️.6.0a0 *], which conflicts with any installable versions previously reported;
├─ spyder [2.3.9|3.1.3|…|3.2.8] would require
│ └─ python =3.6 *, which conflicts with any installable versions previously reported;
├─ spyder [3.1.2|3.1.3] would require
│ └─ rope ==0.9.4 *, which does not exist (perhaps a missing channel);
├─ spyder [3.2.3|3.2.4|…|5.1.5] would require
│ └─ python >=3.6,❤️.7.0a0 *, which conflicts with any installable versions previously reported;
├─ spyder [3.3.0|3.3.1|…|5.3.3] would require
│ └─ python >=3.7,❤️.8.0a0 , which conflicts with any installable versions previously reported;
├─ spyder [6.0.1|6.0.2|6.0.3|6.0.4] would require
│ └─ __osx =
, which is missing on the system;
├─ spyder [6.0.1|6.0.2|6.0.3|6.0.4] would require
│ └─ __linux =
*, which is missing on the system;
├─ spyder 6.0.4 would require
│ └─ spyder-base ==6.0.4 *1 but there are no viable options
│ ├─ spyder-base 6.0.4 would require
│ │ └─ python >=3.10,❤️.11.0a0 *, which conflicts with any installable versions previously reported;
│ ├─ spyder-base 6.0.4 would require
│ │ └─ python >=3.11,❤️.12.0a0 *, which conflicts with any installable versions previously reported;
│ ├─ spyder-base 6.0.4 would require
│ │ └─ python >=3.12,❤️.13.0a0 *, which conflicts with any installable versions previously reported;
│ ├─ spyder-base 6.0.4 would require
│ │ └─ python >=3.9,❤️.10.0a0 , which conflicts with any installable versions previously reported;
│ ├─ spyder-base 6.0.4 would require
│ │ └─ __linux =
, which is missing on the system;
│ └─ spyder-base 6.0.4 would require
│ └─ __osx =
, which is missing on the system;
└─ spyder 5.5.1 would require
└─ package_has_been_revoked =
*, which does not exist (perhaps a missing channel).

3.最终解决

查看本机发现python为最新版本3.13.2,不兼容spyder6.04
为了能安装spyder,卸载python新版本
conda uninstall python3.13.2
安装conda install python
3.11
开始菜单中选择anaconda promt(open_cv)管理员进去安装spyder成功!
在这里插入图片描述

总结

1.Anaconda升级要小心,特别是python升级!
2.卸载、安装packages一定要带版本,比如卸载、安装python时不带版本折腾一下,
3.python解释器设置可以不同版本,也能正常,
4.直接拷贝CV2文件夹也会报错。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值