Jupyter Lab和Notebook问题:内核错误

3
似乎我的某个内核被删除了。也许这就是我遇到这个错误的原因。我该怎么修复它?我能重新安装什么东西(也许是 基础环境 中的内核)吗?
如果没有更好的选择,我会尝试重新安装Anaconda作为最后的手段,具体方法请参考: Jupyter笔记本中的默认内核(Python3)不起作用

enter image description here

我已经尝试使用以下方法:
  1. 安装Jupyter Lab:要求已满足
  2. 运行“jupyter lab”时内核不显示
  3. UI显示“无内核”,但我明确有一个
  4. 在Python3中运行Jupyter Notebook时出现内核错误
  5. https://ipython.readthedocs.io/en/latest/install/kernel_install.html
  6. https://medium.com/flatiron-engineering/recovering-from-a-jupyter-disaster-27401677aeeb

当前内核列表

jupyter kernelspec list

输出:
Available kernels:
  python3    C:\Users\raysu\AppData\Roaming\jupyter\kernels\python3
  vpython    C:\ProgramData\jupyter\kernels\vpython

但我必须有另一个内核(我想可能是某种方式被删除了)与我的基本环境相关联。
C:\Users\raysu\Documents>conda env list
# conda environments:
#
base                  *  C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64
astra_env                C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\envs\astra_env
bokeh_env                C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\envs\bokeh_env
sound_env                C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\envs\sound_env
tf_env                   C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\envs\tf_env
                         C:\Users\raysu\Anaconda3
                         C:\Users\raysu\Anaconda3\envs\pyforge
opencv_env               C:\Users\raysu\AppData\Local\conda\conda\envs\opencv_env
test_env                 C:\Users\raysu\AppData\Local\conda\conda\envs\test_env

错误
Traceback (most recent call last):
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\tornado\web.py", line 1699, in _execute
    result = await result
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\tornado\gen.py", line 742, in run
    yielded = self.gen.throw(*exc_info)  # type: ignore
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\notebook\services\sessions\handlers.py", line 72, in post
    type=mtype))
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\tornado\gen.py", line 735, in run
    value = future.result()
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\tornado\gen.py", line 742, in run
    yielded = self.gen.throw(*exc_info)  # type: ignore
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\notebook\services\sessions\sessionmanager.py", line 88, in create_session
    kernel_id = yield self.start_kernel_for_session(session_id, path, name, type, kernel_name)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\tornado\gen.py", line 735, in run
    value = future.result()
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\tornado\gen.py", line 742, in run
    yielded = self.gen.throw(*exc_info)  # type: ignore
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\notebook\services\sessions\sessionmanager.py", line 101, in start_kernel_for_session
    self.kernel_manager.start_kernel(path=kernel_path, kernel_name=kernel_name)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\tornado\gen.py", line 735, in run
    value = future.result()
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\tornado\gen.py", line 209, in wrapper
    yielded = next(result)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\notebook\services\kernels\kernelmanager.py", line 168, in start_kernel
    super(MappingKernelManager, self).start_kernel(**kwargs)
  File "C:\Users\raysu\AppData\Roaming\Python\Python36\site-packages\jupyter_client\multikernelmanager.py", line 110, in start_kernel
    km.start_kernel(**kwargs)
  File "C:\Users\raysu\AppData\Roaming\Python\Python36\site-packages\jupyter_client\manager.py", line 240, in start_kernel
    self.write_connection_file()
  File "C:\Users\raysu\AppData\Roaming\Python\Python36\site-packages\jupyter_client\connect.py", line 476, in write_connection_file
    kernel_name=self.kernel_name
  File "C:\Users\raysu\AppData\Roaming\Python\Python36\site-packages\jupyter_client\connect.py", line 141, in write_connection_file
    with secure_write(fname) as f:
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\contextlib.py", line 81, in __enter__
    return next(self.gen)
  File "C:\Users\raysu\AppData\Roaming\Python\Python36\site-packages\jupyter_core\paths.py", line 404, in secure_write
    win32_restrict_file_to_user(fname)
  File "C:\Users\raysu\AppData\Roaming\Python\Python36\site-packages\jupyter_core\paths.py", line 359, in win32_restrict_file_to_user
    import win32api
ImportError: DLL load failed: The specified procedure could not be found.


Anaconda让故障排除变得复杂。我几年前就放弃了Anaconda,并且从那以后一直很开心地进行编码。 - Gray
@Gray 我终于解决了。希望这个解决方案能帮助到其他遇到类似“内核错误”问题的人。 - CypherX
3个回答

9
通过编辑 Anaconda 安装后相关的 kernelspec 的 kernel.json 文件解决了问题。
详细的解决方案:
在之前的报告中,我发现我有两个 kernelspecs(命令:jupyter kernelspec list)。
1. python3: C:\Users\raysu\AppData\Roaming\jupyter\kernels\python3 2. vpython: C:\ProgramData\jupyter\kernels\vpython
kernelspec 是一个 .json 文件,用于维护 jupyter-notebook/lab 启动特定内核的内核规范。幸运的是,kernel vpython 能够正常工作,这帮助我缩小了问题的范围,位于 kernelpsec: python3 内的问题。我需要纠正指向 python.exe 的路径,然后问题就解决了。
我想提到,在这种情况下,以下两个资源非常有用。当我看到 kernelspec 文档的典型结构后,我更清楚地知道问题所在。
注意:由于我无法测试这个理论,我建议任何遇到类似问题的人先复制旧的 kernel.json 文件,然后更新指向 python.exe 文件的路径。如果这样解决了内核错误,那么重新安装 Anaconda 可能并不必要。
长话短说:如果您的 Jupyter notebook/lab 环境出现“内核错误”,问题很可能在 kernelspec 文件 kernel.json 中,指向 python.exe 的路径不正确。如果即使重新安装 anaconda/jupyter-notebook/lab 后仍然出现相同的内核错误,这可能是更有可能的情况。
例如,在我的情况下,python3-kernelspec 位于:
"C:\Users\raysu\AppData\Roaming\jupyter\kernels\python3"

python.exe 位于以下位置:

"C:\Users\raysu\Anaconda3\python.exe"

但是,python执行文件的路径是不正确的:它仍然显示旧安装版本的路径。重新安装 Anaconda 之后,这个路径已经改变了。因此,我需要通过编辑 kernel.json 文件来更新 python3 的内核规范。

# kernel.json file
{
 "argv": [
  "C:\\Users\\raysu\\Anaconda3\\python.exe",
  "-m",
  "ipykernel_launcher",
  "-f",
  "{connection_file}"
 ],
 "display_name": "Python 3",
 "language": "python"
}

如果这不能解决你的问题,请参考以下两个文档。我因此浪费了4天时间。希望这个答案能帮助未来的某个人。

有什么有用的信息?

  1. https://jupyter-notebook.readthedocs.io/en/stable/troubleshooting.html
  2. https://jupyter-client.readthedocs.io/en/stable/kernels.html

谢谢!我最初安装了Python 3.10,然后安装了Anaconda,但它只提供了3.8版本。卸载了两者并重新安装了Anaconda,导致了这个问题。很遗憾的是,要彻底干净地卸载它们并不容易。 - alex
@alex 不要在anaconda之外安装Python。一旦你安装了anaconda,你可以通过创建一个新的环境来安装Python 3.10,方法如下:conda create -n py310 python=3.10 -c conda-forge。请参考这个答案了解如何正确使用conda:https://dev59.com/O77pa4cB1Zd3GeqPpgJ0#66560897。 - CypherX
我的Jupyterlab运行了很长一段时间,一天早上突然出现了这个错误。jupyter kernelspec列表中显示了一个项目,其他一切似乎都正常。将jupyter-client从5.3.3升级到7.1.0后,一切又恢复正常了。 - Balepur

2

我能够解决这个问题,请参考这里

我使用如下命令安装了 jupyter_client 5.3.4 和 jupyter_core 4.6.0

pip install jupyter_client --upgrade

那么怎么办呢?问题的解决方案是什么? - J. Murray
@Odunsi 你提到的问题是指PermissionError: [Errno 13] Permission denied #4907。看起来这与此处提出的问题中提到的内核错误不同,对吗? - CypherX
不是的。但我在想也许它们之间有关联。 - Odunsi

0
最受欢迎的答案是正确的,即使你和我一样遇到了[Winerror 2]的问题。进入内核JSON并将路径更改为Anaconda3 python.exe,问题很可能就会得到解决。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接