【问题标题】:Jupyter Lab and Notebook Problem: Kernel ErrorJupyter Lab 和 Notebook 问题:内核错误
【发布时间】:2020-02-15 23:03:42
【问题描述】:

似乎我的一个内核被删除了。也许这就是我收到此错误的原因。我怎么可能修复它?我可以重新安装一些东西(也许是基础环境中的内核)?

如果没有更好的选择,我会尝试完全重新安装 anaconda 作为最后的手段:Default kernel in jupyter notebook (Python3) not working

我尝试过使用以下方法:

  1. Installation of Jupyter Lab: requirements already satisfied
  2. Kernels don't show up when runnning "jupyter lab"
  3. UI says "No kernel" while I definitively have one
  4. Getting Kernel Error while running Jupyter Notebook in Python3
  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 我终于能够解决它。我希望该解决方案可以帮助遇到类似kernel error 问题的其他人。

标签: python jupyter-notebook kernel python-3.6 jupyter-lab


【解决方案1】:

在全新安装 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 启动某个内核的内核规范。幸运的是,内核 vpython 运行正常,这帮助我缩小了 kernelpsec: python3 内部问题的根源。我必须更正 python 可执行文件 (python.exe) 的路径,这解决了这个问题。

我想提一下,在这种情况下,以下两个资源会派上用场。在我看到kernelspec 文档的典型结构 之后,我更清楚问题可能出在哪里。

注意:但是,由于我无法测试这个理论,我会问其他有类似问题的人,先复制旧的 kernel.json 文件,然后将路径更新为 @ 987654337@ 文件。如果这样可以解决内核错误,则可能不需要完全重新安装 Anaconda。

长话短说:

如果您的 jupyter notebook/lab 环境显示内核错误,则问题的根源很可能在 kernelspec 文件中:kernel.json strong>python.exe 不正确。如果重新安装 anaconda/jupyter-notebook/lab 后相同的 kernel error 仍然存在,这可能是一种更可能的情况。

例如,在我的例子中,python3-kernelspec 位于:

"C:\Users\raysu\AppData\Roaming\jupyter\kernels\python3"

python.exe 位于:

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

但是,python-executable 的路径是错误的:它仍然显示旧安装的路径。在 anaconda-reinstallation 之后,这条路径已经改变。因此,我不得不通过编辑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 会导致此问题。不幸的是,要制作一个真正干净的 unistall 并不容易。
  • @alex 不要在 anaconda 之外安装 python。安装 anaconda 后,您可以通过创建一个新环境来安装 python 3.10,如下所示:conda create -n py310 python=3.10 -c conda-forge。请参阅有关如何正确使用 conda 的答案:stackoverflow.com/a/66560897/8474894.
  • 我的 Jupyterlab 在很长一段时间内都可以正常工作,一个晴朗的早晨,我收到了这个错误。 jupyter kernelspec 列表向我显示了列表中的一项,其他一切似乎都很好。将 jupyter-client 从 5.3.3 升级到 7.1.0 后一切正常。
【解决方案2】:

我能够解决问题 here

我用这个命令安装了 jupyter_client 5.3.4 和 jupyter_core 4.6.0

pip install jupyter_client --upgrade

【讨论】:

  • 那怎么办?问题的解决方案是什么?
  • @Odunsi 你提到的问题是指PermissionError: [Errno 13] Permission denied #4907。它看起来不像这里的问题中提到的同一个内核错误。是吗?
  • 它没有。但我在想也许它们都是相关的。
猜你喜欢
  • 2022-12-31
  • 2020-07-17
  • 1970-01-01
  • 1970-01-01
  • 2020-08-25
  • 2019-03-08
  • 2021-04-01
  • 2020-10-22
  • 1970-01-01
相关资源
最近更新 更多