Python3.10导入undetected-chromedriver无法从collections导入Mapping如何解决
问题原因
- 该报错是Python版本兼容性导致的:Python 3.3起就将
Mapping、Sequence等抽象基类迁移到了collections.abc子模块下,Python 3.10及以上版本彻底移除了从collections直接导入这些类的路径,你当前使用的undetected_chromedriver版本较低,未适配Python 3.10+的规则。 - 注意:
collections和collections.abc都是Python内置标准库模块,不需要额外通过pip安装,第三方包列表中搜索不到属于正常情况。
可行解决方法
方法1:升级undetected_chromedriver到最新版本(最推荐)
最新版的undetected_chromedriver已经完成了Python 3.10+的适配,直接执行升级命令即可:
pip install --upgrade undetected-chromedriver
升级完成后可以将原导入语句简化为以下形式,无需再指定v2分支:
import undetected_chromedriver as uc driver = uc.Chrome() resp = driver.get("https://google.com")
方法2:手动修改源码(仅适用于无法升级版本的场景)
找到报错提示的cdp.py文件,路径为:C:\Users\Narmu\Desktop\Python_Workspace_Master\venv\lib\site-packages\undetected_chromedriver\cdp.py
将文件中原有导入语句:
from collections import Mapping, Sequence
修改为:
from collections.abc import Mapping, Sequence
保存文件后重新运行代码即可。
内容的提问来源于stack exchange,提问作者Peace_in_process
相关产品推荐
相关产品推荐

