jupyter-notebook 无法识别任何内核,尤其是仅为其虚拟环境选择的内核[重复]

Posted

技术标签:

【中文标题】jupyter-notebook 无法识别任何内核,尤其是仅为其虚拟环境选择的内核[重复]【英文标题】:jupyter-notebook fails to recognize any kernel, especially the kernel selected just for its virtual environment [duplicate] 【发布时间】:2021-10-09 20:22:31 【问题描述】:

我花了几天时间来解决 conda 的 python 虚拟环境识别它们所构建的内核的问题。该问题表现为 Spyder 和 Jupyter-notebook 都无法以有效的内核启动。下面是一个为内核 3.8.10 构建的 conda 环境示例,专门用于运行 pymc3(根据 pymc3 环境要求),以及它在 jupyter-notebook 中如何失败,也专门在该环境中选择和安装。

我唯一确定的是:错误消息完全具有误导性。 win3api dll好像和实际问题没有关系

有人认识这个吗?是否有一些调整可以让我继续前进?或者这是另一个 Rip-out-and-reinstall-Anaconda3 案例?这是一个熟悉的副歌。

##########################

Failed to start the kernel
Unhandled error

Traceback (most recent call last):
  File "C:\Users\Peter\AppData\Roaming\Python\Python38\site-packages\tornado\web.py", line 1704, in _execute
    result = await result
  File "C:\Users\Peter\AppData\Roaming\Python\Python38\site-packages\tornado\gen.py", line 769, in run
    yielded = self.gen.throw(*exc_info)  # type: ignore
  File "C:\Users\Peter\anaconda3\lib\site-packages\notebook\services\sessions\handlers.py", line 69, in post
    model = yield maybe_future(
  File "C:\Users\Peter\AppData\Roaming\Python\Python38\site-packages\tornado\gen.py", line 762, in run
    value = future.result()
  File "C:\Users\Peter\AppData\Roaming\Python\Python38\site-packages\tornado\gen.py", line 769, in run
    yielded = self.gen.throw(*exc_info)  # type: ignore
  File "C:\Users\Peter\anaconda3\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:\Users\Peter\AppData\Roaming\Python\Python38\site-packages\tornado\gen.py", line 762, in run
    value = future.result()
  File "C:\Users\Peter\AppData\Roaming\Python\Python38\site-packages\tornado\gen.py", line 769, in run
    yielded = self.gen.throw(*exc_info)  # type: ignore
  File "C:\Users\Peter\anaconda3\lib\site-packages\notebook\services\sessions\sessionmanager.py", line 100, in start_kernel_for_session
    kernel_id = yield maybe_future(
  File "C:\Users\Peter\AppData\Roaming\Python\Python38\site-packages\tornado\gen.py", line 762, in run
    value = future.result()
  File "C:\Users\Peter\anaconda3\lib\site-packages\notebook\services\kernels\kernelmanager.py", line 176, in start_kernel
    kernel_id = await maybe_future(self.pinned_superclass.start_kernel(self, **kwargs))
  File "C:\Users\Peter\AppData\Roaming\Python\Python38\site-packages\jupyter_client\multikernelmanager.py", line 186, in start_kernel
    km.start_kernel(**kwargs)
  File "C:\Users\Peter\AppData\Roaming\Python\Python38\site-packages\jupyter_client\manager.py", line 337, in start_kernel
    kernel_cmd, kw = self.pre_start_kernel(**kw)
  File "C:\Users\Peter\AppData\Roaming\Python\Python38\site-packages\jupyter_client\manager.py", line 286, in pre_start_kernel
    self.write_connection_file()
  File "C:\Users\Peter\AppData\Roaming\Python\Python38\site-packages\jupyter_client\connect.py", line 466, in write_connection_file
    self.connection_file, cfg = write_connection_file(self.connection_file,
  File "C:\Users\Peter\AppData\Roaming\Python\Python38\site-packages\jupyter_client\connect.py", line 136, in write_connection_file
    with secure_write(fname) as f:
  File "C:\Users\Peter\anaconda3\lib\contextlib.py", line 113, in __enter__
    return next(self.gen)
  File "C:\Users\Peter\AppData\Roaming\Python\Python38\site-packages\jupyter_core\paths.py", line 461, in secure_write
    win32_restrict_file_to_user(fname)
  File "C:\Users\Peter\AppData\Roaming\Python\Python38\site-packages\jupyter_core\paths.py", line 387, in win32_restrict_file_to_user
    import win32api
ImportError: DLL load failed while importing win32api: The specified procedure could not be found.
​```

【问题讨论】:

请详细说明您的设置。看起来 Jupyter 是本机安装的(不是通过 Conda 安装的),但您正在尝试将 Conda 环境作为内核运行。您是如何将 Conda 环境注册为内核的? 【参考方案1】:

首先,确保使用conda activate myenv 激活您的环境

接下来,安装 ipykernel,它为 Jupyter 提供 IPython 内核:

pip install --user ipykernel

接下来,您可以通过键入以下内容将虚拟环境添加到 Jupyter:

python -m ipykernel install --user --name=myenv

这应该打印以下内容:

Installed kernelspec myenv in /home/user/.local/share/jupyter/kernels/myenv

在此文件夹中,您将找到一个 kernel.json 文件,如果您正确执行所有操作,该文件应如下所示:


"argv": [
     "/home/user/anaconda3/envs/myenv/bin/python",
     "-m",
     "ipykernel_launcher",
     "-f",
     "connection_file"
    ],
"display_name": "myenv",
"language": "python"

就是这样!

【讨论】:

我听从了您的建议,并且没有发生任何事故。我什至在那个环境中做了一个新鲜的环境和pymc3。这些步骤如宣传的那样工作。我还必须 conda install -c conda-forge jupyter 因为 vanilla 3.8 内核套件没有附带。所有文件都存在并按照您的描述进行配置。全新的 jupyter-notebook 开箱即用。同样的内核错误。 也许 jupyter 不是为在虚拟环境中运行而设计的。这与迄今为止的所有观察结果一致。 我认为问题在于 anaconda/conda 实用程序不能被信任来创建一个包含它声称支持的内核的工作版本和/或它声称要添加的各种实用程序的虚拟环境。安装了 jupyter-notebook 等实用程序的新 anaconda 环境不能直接配置。这似乎是 anaconda 项目本身的一个主要限制。 我放弃了 Windows 和虚拟环境。我在 linux 中有一个默认环境,它没有给我带来任何问题。 Jupyter 适用于默认内核 3.8.10。 Venv 显然不受支持,但我很高兴我不需要它。

以上是关于jupyter-notebook 无法识别任何内核,尤其是仅为其虚拟环境选择的内核[重复]的主要内容,如果未能解决你的问题,请参考以下文章

Jupyter-notebook安装报错KernelRestarter: restart failed

在 jupyter 中选择 python 内核

在Linux中插上U盘,是怎么识别和挂载U盘的,Linux底层里发生了啥变化!急急急!

CodeLite 无法识别任何 c++ 代码

PySpark 和 Jupyter-notebook 中的 Collect() 错误

在 Ubuntu 上启动时运行 Jupyter-notebook