Anaconda, Jupyter Notebook 和 MissingIDFieldWarning

huangapple go评论215阅读模式
英文:

Anaconda, Jupyter Notebook and MissingIDFieldWarning

问题

我已经安装了Anaconda 3。当我运行Jupyter Notebook并保存一些东西时,出现了下一个问题:

  1. C:\ProgramData\Anaconda3\lib\site-packages\nbformat_init_.py:128: MissingIDFieldWarning: 代码单元缺少id字段,这将在将来的nbformat版本中变为严重错误。在验证之前,您可以使用normalize()对您的笔记本进行规范化(自nbformat 5.1.4以来可用)。nbformat的旧版本会在背后透明地修复此问题,但将来不会再这样做。
    validate(nb)

  2. C:\ProgramData\Anaconda3\lib\site-packages\notebook\services\contents\manager.py:353: MissingIDFieldWarning: 代码单元缺少id字段,这将在将来的nbformat版本中变为严重错误。在验证之前,您可以使用normalize()对您的笔记本进行规范化(自nbformat 5.1.4以来可用)。nbformat的旧版本会在背后透明地修复此问题,但将来不会再这样做。
    validate_nb(model['content'])
    [I 23:27:05.005 NotebookApp] Starting buffering for 956f2cf8-4baa-4f41-a715-89fc3e1b078c:7228c4b40f9241eb968ac5f8c1af

其中python -->
C:\ProgramData\Anaconda3\python.exe
C:\Users\Mitko Stoychev\AppData\Local\Programs\Python\Python310\python.exe
C:\Users\Mitko Stoychev\AppData\Local\Microsoft\WindowsApps\python.exe

其中jupyter
(base) --> C:\Users\Mitko Stoychev>where jupyter
C:\ProgramData\Anaconda3\Scripts\jupyter.exe

英文:

I have already intalled Anaconda 3.When I run Jupyter Notebook and save something. The next problem appears:

  1. C:\ProgramData\Anaconda3\lib\site-packages\nbformat_init_.py:128: MissingIDFieldWarning: Code cell is missing an id field, this will become a hard error in future nbformat versions. You may want to use normalize() on your notebooks before validations (available since nbformat 5.1.4). Previous versions of nbformat are fixing this issue transparently, and will stop doing so in the future.
    validate(nb)

  2. C:\ProgramData\Anaconda3\lib\site-packages\notebook\services\contents\manager.py:353: MissingIDFieldWarning: Code cell is missing an id field, this will become a hard error in future nbformat versions. You may want to use normalize() on your notebooks before validations (available since nbformat 5.1.4). Previous versions of nbformat are fixing this issue transparently, and will stop doing so in the future.
    validate_nb(model['content'])
    [I 23:27:05.005 NotebookApp] Starting buffering for 956f2cf8-4baa-4f41-a715-89fc3e1b078c:7228c4b40f9241eb968ac5f8c1af

where python -->
C:\ProgramData\Anaconda3\python.exe
C:\Users\Mitko Stoychev\AppData\Local\Programs\Python\Python310\python.exe
C:\Users\Mitko Stoychev\AppData\Local\Microsoft\WindowsApps\python.exe

where jupyter
(base) --> C:\Users\Mitko Stoychev>where jupyter
C:\ProgramData\Anaconda3\Scripts\jupyter.exe

答案1

得分: 3

这个警告的意思是,你的某个单元格缺少一个"id字段",目前会产生一个MissingIDFieldWarning警告,但在将来的nbformat版本中将成为一个严格的错误。你可以在文本编辑器中打开相关的Jupyter笔记本,应该会看到每个单元格都有一个"id"字段,例如:

"cells": [
  {
   "cell_type": "code",
   "execution_count": 1,   
   "id": "0945defe",
   ...},
...]

显然,至少有一个单元格缺少这个字段,尽管它是一个必填字段,应该自动生成。

你已经收到的警告已经包含了如何解决这个问题的建议:

import nbformat

with open("problematic_notebook.ipynb", "r") as file:
    nb_corrupted = nbformat.reader.read(file)

nbformat.validator.validate(nb_corrupted)
# <stdin>:1: MissingIDFieldWarning: Code cell is missing an id field, 
# this will become a hard error in future nbformat versions. 
# You may want to use `normalize()` on your notebooks before validations (available since nbformat 5.1.4). 
# Previous versions of nbformat are fixing this issue transparently, and will stop doing so in the future.

nb_fixed = nbformat.validator.normalize(nb_corrupted)
nbformat.validator.validate(nb_fixed[1])
# Produces no warnings or errors.

with open("fixed_notebook.ipynb", "w") as file:
    nbformat.write(nb_fixed[1], file)
英文:

It is not clear what your question was, so I will explain this warning and how to fix it.

For whatever reason, one of your cells is "missing an id field", which for now produces a MissingIDFieldWarning, but "will become a hard error in future nbformat versions".
You can open the Jupyter notebook in question in a text editor, where you should see an id field for each cell, e.g.:

&quot;cells&quot;: [
  {
   &quot;cell_type&quot;: &quot;code&quot;,
   &quot;execution_count&quot;: 1,   
   &quot;id&quot;: &quot;0945defe&quot;,
   ...},
...]

Apparently this field was missing for (at least) one of the cells, even though it is a mandatory field and should be produced automatically.

The warning you got already includes a suggestion on how to fix this problem:

import nbformat

with open(&quot;problematic_notebook.ipynb&quot;, &quot;r&quot;) as file:
    nb_corrupted = nbformat.reader.read(file)

nbformat.validator.validate(nb_corrupted)
# &lt;stdin&gt;:1: MissingIDFieldWarning: Code cell is missing an id field, 
# this will become a hard error in future nbformat versions. 
# You may want to use `normalize()` on your notebooks before validations (available since nbformat 5.1.4). 
# Previous versions of nbformat are fixing this issue transparently, and will stop doing so in the future.

nb_fixed = nbformat.validator.normalize(nb_corrupted)
nbformat.validator.validate(nb_fixed[1])
# Produces no warnings or errors.

with open(&quot;fixed_notebook.ipynb&quot;, &quot;w&quot;) as file:
    nbformat.write(nb_fixed[1], file)

huangapple
  • 本文由 发表于 2023年3月4日 06:41:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/75632445.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定