[解决] 问题:ImportError: cannot import name ‘Callable‘ from ‘collections‘

问题

我在运行yolov8的代码时,出现了ImportError: cannot import name 'Callable' from 'collections'的错误

原因

版本问题:以下collections的方法都在Python3.10版本后被取消了

 ["Awaitable", "Coroutine", "AsyncIterable", "AsyncIterator", "AsyncGenerator", "Hashable", "Iterable", "Iterator", "Generator", "Reversible", "Sized", "Container", "Callable", "Collection", "Set", "MutableSet", "Mapping", "MutableMapping", "MappingView", "KeysView", "ItemsView", "ValuesView", "Sequence", "MutableSequence", "ByteString"]

解决

”from collections import ***“,在3.6版本之前collections后面是不需要带.abc的,3.7之后就会提醒需要加.abc,现在3.10版本停止使用不加.abc的情况。

所以正确的写法应该是“from collections.abc import ***",根据上图的错误提示,红框内需要修改添加.abc。

  • 20
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 9
    评论
"cannot import name 'Container' from 'collections'" 的错误通常出现在Python 3.10及更高版本中,因为从这个版本开始,库collections不再导入名为Container的对象。这是因为Python 3.10引入了新的库collections.abc,它取代了collections模块中的某些对象。因此,如果您希望在Python 3.10及更高版本中使用Container对象,可以尝试从collections.abc中导入它,如下所示: ```python from collections.abc import Container ``` 这样,您就可以成功导入'Container'对象,并在您的代码中使用它了。请注意,如果您的代码需要兼容Python 3.10之前的版本,您可以使用类似以下的代码: ```python try: from collections.abc import Container except ImportError: from collections import Container ``` 这样,如果在Python 3.10及更高版本中,您可以导入'Container'对象,否则将会导入collections模块中的'Container'对象。这样可以确保您的代码在不同版本的Python上都能正常运行。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [001 ImportError: cannot import name ‘Iterable‘ from ‘collections](https://blog.csdn.net/qq_55285829/article/details/127577631)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值