Python导入SkopeRules报错:无法从collections导入Iterable
解决SkopeRules导入时的Iterable错误
问题根源
skope-rules旧版本的源代码里,仍直接从collections模块导入Iterable,但Python 3.10及以上版本中,Iterable已迁移至collections.abc模块。你自己导入collections.abc.Iterable无法修正库内部代码的问题,因此报错依然存在。
可行解决方案
方案1:安装修复后的分支版本
执行以下命令安装社区修复的适配Python 3.10+的版本:
!pip install git+https://github.com/scikit-learn-contrib/skope-rules.git@fix/python310
方案2:手动修改库源码
若无法安装修复版本,可直接修改skope-rules的源码文件:
- 找到Anaconda环境中skope-rules的安装目录,路径类似
C:\ProgramData\anaconda3\Lib\site-packages\skrules - 打开该目录下的
utils.py文件 - 将文件开头的
from collections import Iterable替换为:try: from collections.abc import Iterable except ImportError: from collections import Iterable - 保存文件后,重启Jupyter Notebook再尝试导入
方案3:降级Python版本(不推荐)
将Python版本降至3.9及以下,此时collections模块仍包含Iterable,可兼容旧版skope-rules。
内容的提问来源于stack exchange,提问作者Achmed S Koroma
相关产品推荐
相关产品推荐

