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

新环境运行带ipywidgets的Jupyter Notebook报IPython未定义错误

解决Jupyter Notebook中“Javascript Error: IPython is not defined”问题
  • 检查内核与环境匹配
    终端执行以下命令查看内核路径,确认Python 3内核指向你的base环境:

    jupyter kernelspec list
    

    如果路径不匹配,删除旧内核并重新安装关联base环境的内核:

    jupyter kernelspec remove python3
    python -m ipykernel install --user --name python3 --display-name "Python 3 (base)"
    
  • 重新安装ipympl并启用扩展
    先卸载现有ipympl,再通过conda-forge渠道重新安装,同时启用相关Jupyter扩展:

    conda uninstall -y ipympl
    conda install -c conda-forge ipympl
    jupyter nbextension enable --py --sys-prefix ipympl
    jupyter nbextension enable --py --sys-prefix widgetsnbextension
    
  • 清理Jupyter缓存
    删除Jupyter的缓存文件,避免旧资源残留导致的兼容性问题:

    jupyter clean --all
    
  • 更换matplotlib后端测试
    将代码中的%matplotlib nbagg替换为%matplotlib widget,新版Jupyter中widget后端的兼容性更稳定:

    %matplotlib widget
    import matplotlib.pyplot as plt
    fig, ax = plt.subplots()
    
  • 验证环境一致性
    在Notebook中运行以下代码,检查内核使用的IPython版本是否和终端的8.30.3一致:

    import IPython
    print(IPython.__version__)
    

    如果版本不符,重复第一步重新安装内核。

内容的提问来源于stack exchange,提问作者iambirdy

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.12 19:52:36