【问题标题】:How to silence warning from import IPython.nbformat: "UserWarning: nbformat.current is deprecated."如何消除来自导入 IPython.nbformat 的警告:“UserWarning:nbformat.current 已弃用。”
【发布时间】:2021-04-05 15:57:25
【问题描述】:

我正在使用用于导入 IPython (Jupyter) 笔记本的示例代码,
Importing Notebooks。示例代码仍然可以正常运行,但是它会生成一个我想理解和修复的警告:

site-packages/nbformat/current.py:15: UserWarning: nbformat.current is deprecated.

- use nbformat for read/write/validate public API
- use nbformat.vX directly to composing notebooks of a particular version

  warnings.warn("""nbformat.current is deprecated.

至少从 2015 年开始就已经讨论过这个警告,但我找不到任何关于如何处理它的建设性建议。这是一个可以通过修复代码来解决的警告,还是一个会从 IPython 中消失而无需替换的函数?

如果您点击指向 IPython 博客的链接,他们声称有更新版本,但他们的链接指向一个不存在的页面。

此代码示例在 Stack Overflow 的其他线程中被广泛讨论,例如

python access functions in ipython notebook

【问题讨论】:

  • 该链接指向一个不存在的页面,因为 v5.7.6 文档不包含您指向的示例部分 - 您的链接适用于 4.x,它已经很老了。 IIUC 这是您要查找的页面:jupyter-notebook.readthedocs.io/en/5.7.6/examples/Notebook/…
  • 感谢您的链接。我会研究从 4.x 到 5.7.6 的变化,并报告回来。

标签: python-3.x jupyter-notebook ipython


【解决方案1】:

请记住,您链接的示例适用于更旧版本的 Jupyter - 4.x。包含这些示例的页面已在某个时候重新定位,对于 Jupyter 的 5.7.6 版本(撰写本文时的最新版本),它位于 here

首先,将from IPython.nbformat import current 导入替换为from nbformat import read

然后,替换4.x snippet的这一部分:

        with io.open(path, 'r', encoding='utf-8') as f:
            nb = current.read(f, 'json')

newer version:

        with io.open(path, 'r', encoding='utf-8') as f:
            nb = read(f, 4) 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-24
    • 2020-03-29
    相关资源
    最近更新 更多