Jupyter Notebook导入部分Python库触发RecursionError求助
导入Python库触发RecursionError的解决建议
问题描述
新电脑搭建环境后,Jupyter Notebook中导入pandas、numpy等基础库正常,但导入ruptures等依赖scipy的库时,触发RecursionError: maximum recursion depth exceeded while getting the repr of an object。报错栈显示递归发生在numpy的dtype.__repr__和np.issubdtype之间,调整sys.setrecursionlimit无效果。
解决步骤
降级numpy至兼容版本
该递归问题多因numpy高版本与Python 3.11的兼容性bug导致,建议降级到稳定兼容版本:# 使用pip pip install numpy==1.24.3 # 或使用conda conda install numpy=1.24.3重建虚拟环境消除依赖冲突
当前环境可能存在依赖版本不匹配,彻底重建环境:# 删除现有环境 conda remove -n reg_env --all # 创建新环境并安装兼容依赖 conda create -n reg_env python=3.10 numpy=1.24 scipy=1.10 ruptures -y conda activate reg_env # 安装Jupyter pip install jupyter升级相关依赖包
确保scipy和ruptures为最新稳定版,与numpy版本匹配:pip install --upgrade scipy ruptures验证环境并修复Jupyter内核关联
先在终端(非Jupyter)中测试导入,确认环境本身无问题:import numpy as np import scipy.stats.mstats import ruptures若终端导入正常但Jupyter报错,说明内核未关联正确环境,重新安装内核:
conda activate reg_env pip install ipykernel python -m ipykernel install --user --name=reg_env启动Jupyter后,在内核选项中切换到
reg_env即可。
内容的提问来源于stack exchange,提问作者Roy Yanovski
相关产品推荐
相关产品推荐

