在Anaconda中运行jupyter themes时遇AttributeError错误求助
解决jupyterthemes运行jt命令报错AttributeError: module 'collections' has no attribute 'Iterable'
问题原因
Python 3.10及以上版本中,collections.Iterable已被迁移至collections.abc.Iterable,而jupyterthemes依赖的lesscpy包旧版本未适配该变更,导致运行jt命令时触发错误。
解决方案
方案1:手动修复lesscpy代码文件
- 定位到报错文件路径:
C:\Users\erfan\anaconda3\lib\site-packages\lesscpy\lessc\utility.py - 用文本编辑器打开该文件,找到第28行代码:
if isinstance(elm, collections.Iterable) and not isinstance(elm, string_types): - 将其修改为:
if isinstance(elm, collections.abc.Iterable) and not isinstance(elm, string_types): - 若文件顶部未导入
collections.abc,可在导入区域添加:
并将第28行简化为:from collections.abc import Iterableif isinstance(elm, Iterable) and not isinstance(elm, string_types):
方案2:升级/替换lesscpy版本
- 先卸载旧版本lesscpy:
pip uninstall -y lesscpy - 安装最新兼容版本:
若PyPI最新版本仍不兼容,可尝试安装修复后的lesscpy源码包(需自行获取适配Python3.10+的源码)pip install lesscpy --upgrade
方案3:降级Python版本(不推荐)
若上述方案无效,可创建Python3.9及以下版本的conda环境:
- 创建环境:
conda create -n py39 python=3.9 - 激活环境:
conda activate py39 - 重新安装jupyter及jupyterthemes:
conda install jupyter && pip install jupyterthemes
验证
完成操作后,运行任意jt命令测试,例如:
jt -t monokai -f fira -fs 13
若命令正常执行,且Jupyter Notebook主题成功变更,则问题解决。
内容的提问来源于stack exchange,提问作者Erfan Moosavi
相关产品推荐
相关产品推荐

