Skip to main content
 首页 » 编程设计

python之IPython.html.widgets 在 IPython 中说 : no module named widgets

2025年04月02日13dflying

所有人的上帝日。我正在尝试在 Jupyter + IPython 环境中执行 Cirille Rossant 的书“IPython Interactive Computing an Visualizing Cookbook”中的代码。

在我尝试之前一切正常:

from collections import OrderedDict 
from IPython.display import (display, clear_output, YouTubeVideo) 
from IPython.html.widgets import DropdownWidget 

如果我重启内核会出现这个消息:

/usr/local/lib/python3.4/dist-packages/IPython/html.py:14: ShimWarning: The `IPython.html` package has been deprecated. You should import from `notebook` instead. `IPython.html.widgets` has moved to `ipywidgets`. 
  "`IPython.html.widgets` has moved to `ipywidgets`.", ShimWarning) 

然后我尝试通过以下方式更改第三行(问题):

from ipywidgets import DropdownWidgets 

但 Jupyter 回答:ImportError: cannot import name 'DropdownWidget'

有人可以帮助我吗?

接下来是部分回答

嗯,在网上找我在这个页面找到ipywidgets installation我的问题的答案。

我尝试了下一个示例并且工作正常:

import numpy as np 
url = "http://donnees.ville.montreal.qc.ca/dataset/f170fecc-18db-44bc-b4fe-5b0b6d2c7297/resource/ec12447d-6b2a-45d0-b0e7-fd69c382e368/download/2013.csv" 
df = pd.read_csv(url, index_col='Date', parse_dates=True, dayfirst=True) 
from ipywidgets import interact 
@interact 
def plot(n=(1, 30)): 
    pd.rolling_mean(df['Berri1'], n).dropna().plot() 
    plt.ylim(0, 8000) 
    plt.show() 

但 jupyter 声称我必须这样做:jupyter nbextension enable --py --sys-prefix widgetsnbextension

我按照建议进入终端,jupyter 响应:

Configure an nbextension to be automatically loaded 
 
Options 
------- 
 
Arguments that take values are actually convenience aliases to full 
Configurables, whose aliases are listed on the help line. For more information 
on full configurables, see '--help-all'. 
 
--debug 
    set log level to logging.DEBUG (maximize logging output) 
-y 
    Answer yes to any questions instead of prompting. 
--generate-config 
    generate default config file 
--section=<Unicode> (EnableNBExtensionApp.section) 
    Default: 'notebook' 
    Which config section to add the extension to. 'common' will affect all 
    pages. 
 
To see all available configurables, use `--help-all` 
 
[EnableNBExtensionApp] CRITICAL | Bad config encountered during initialization: 
[EnableNBExtensionApp] CRITICAL | Unrecognized flag: '--py' 

在 1 中查看有建议:jupyter nbextension enable --py widgetsnbextension 但在控制台中建议 jupyter nbextension enable --py --sys-prefix widgetsnbextension。请注意,有一点不同,但在这两种情况下都出现关于 --py 选项的相同错误

此刻问题得到了部分回答。

请您参考如下方法:

好吧,在网上搜索我在 jupyter 网页 ( jupyter ) 上找到了这本书的重新访问的笔记本,并且几乎运行良好(除了一些异常(exception),至少在第 01 章,我去尝试使用其他章节)。