Jupyter notebook 和虚拟环境的奇怪行为

Odd behavior with Jupyter notebook and virtual environment

我在 Ubuntu 16-04(LTS 版本)上安装了 anaconda python 发行版,我想在 jupyter notebooks 上使用虚拟环境,但我遇到了一些奇怪的行为:

conda update conda

conda create -n myvirtenv python=3.6 anaconda

conda activate myvirtenv

为 jupyter 添加了虚拟环境

python -m ipykernel install --user –name=myvirtenv

当我从 default 环境启动 jupyter notebook 并获得 python 版本时:

import sys

print(sys.version)
3.7.4 (default, Aug 13 2019, 20:35:49) 
[GCC 7.3.0]

这是预期的,但是当我转到 Kernel > change kernel 和 select myvirtenv 我得到相同的输出.

当我激活 myvirtenv 并使用默认内核启动 jupyter notebook 时,我得到以下输出:

print(sys.version)
3.6.10 |Anaconda, Inc.| (default, Jan  7 2020, 21:14:29) 
[GCC 7.3.0]

当我将内核更改为 myvirtenv 时,我得到以下输出:

print(sys.version)
3.7.4 (default, Aug 13 2019, 20:35:49) 
[GCC 7.3.0]

我希望当我从 default 环境或 myvirtenv select 内核 myvirtenv 启动 jupyter notebook 时会使用 python 3.6.10 而默认内核会使用 python 3.7.4?我该怎么做才能确保内核 myvirtenv 使用正确的虚拟环境?

我找到了:

kernel.json 文件指向错误 python:

{
 "argv": [
  "/home/****/anaconda3/bin/python",
  "-m",
  "ipykernel_launcher",
  "-f",
  "{connection_file}"
 ],
 "display_name": "myvirtenv",
 "language": "python"
}

更改为:

{
 "argv": [
  "/home/****/anaconda3/envs/myvirtenv/bin/python",
  "-m",
  "ipykernel_launcher",
  "-f",
  "{connection_file}"
 ],
 "display_name": "myvirtenv",
 "language": "python"
}