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

如何解决Jupyter Notebook中No module named 'IPython.html'错误?

问题分析与解决

核心问题:导入路径过时

你遇到的ModuleNotFoundError是因为教程使用了旧版Jupyter的导入路径。IPython.html.widgets是IPython 3.x及更早版本的写法,现在Jupyter交互式组件已独立为ipywidgets包,导入路径完全变更。

分步解决步骤

1. 安装/更新依赖包

确保ipywidgets及相关依赖已安装,在终端或Jupyter命令行执行:

pip install ipywidgets
jupyter nbextension enable --py widgetsnbextension

若使用conda环境,执行:

conda install -c conda-forge ipywidgets

2. 修正代码中的导入与错误

你的代码还有几处缺失导入和变量错误,修正后的完整代码如下:

# 导入正确的交互式组件模块
from ipywidgets import interact
# 导入numpy和matplotlib的必要组件
import numpy as np
import matplotlib.pyplot as plt

t = np.arange(0.0, 1.0, 0.01)

def pltsin(f):
    # 修正未定义的x变量,替换为已创建的t数组
    plt.plot(t, np.sin(2 * np.pi * t * f))
    plt.show()
    
interact(pltsin, f=(1,10,0.1))

代码修正点说明

  • 替换过时的from IPython.html.widgets import *为from ipywidgets import interact(按需导入更规范,避免全局污染)
  • 补充导入numpy(arange、sin、pi均来自该库)和matplotlib.pyplot(plt的来源)
  • 修正plt.plot(x, ...)中的未定义变量x,替换为已初始化的t数组

验证运行

重启Jupyter内核后运行修正后的代码,即可正常生成交互式正弦波可视化控件。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.21 20:51:21