太多人问了,专门写一篇吧

太长不看版

  1. 切到新建的那个 Conda 环境下:conda activate xxxxxx
  2. 安装 Python 包 ipykernelpip install ipykernel
  3. 创建 Jupyter Kernel:python -m ipykernel install --user --name=xxxxxx
  4. 刷新页面,JupyterLab 或者 Jupyter Notebook 就会正常看到新的 Kernel 了

了解一下原理

首先一台电脑上可以有多个 Python 环境,比如有 Python 3.7 和 Python 3.8,这很常见。

那如果使用 Python 3.7 安装了 JupyterLab,那么现在打开 JupyterLab 后,只会看到一个 Kernel,这个 Kernel 就是 Python 3.7 的 Kernel。

现在的需求是:我们还希望在这个 JupyterLab 上使用 Python 3.8 的 Kernel,应该怎么做?

如果在本地,那么很多人是直接在终端中切换到 Python 3.8 的环境下,然后再安装 JupyterLab,这样就可以看到 Python 3.8 的 Kernel 了(但是又可能看不到 3.7 的 Kernel)。

但这么做其实不是特别方便,特别在类似 Featurize 这样的云端服务器上,就不得不在本身自带的 JupyterLab 上添加一个 Kernel了。

JupyterLab 实际上是支持通过多环境配置的,在终端中使用 jupyter kernelspec list 就能查看到当前 JupyterLab 支持的 Kernel 列表。(注意:这个命令的执行需要在安装 jupyterlab 那个环境下),其他的环境可能连 jupyter 命令都没有)。因此我们只需要添加一个 kernel 的配置,然后配置置顶到我们新创建的环境即可。

ipykernel 这个工具,就是帮助我们添加 Kernel 配置文件的工具,所以我们需要安装这个工具,当然也可以手动添加配置文件,就不多赘述这种方式了。