You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 15:50:52