Jupyter notebook 运行环境创建和切换 (Win10+Anaconda)

1. **Jupyter notebook运行需要的虚拟环境

activate pytorch-gpu  

pytorch-gpu 为环境名

2. 在当前环境下安装 Jupyter

(pytorch-gpu) C:\Users\xxx> conda install jupyter

启动 Jupyter

(pytorch-gpu) C:\Users\xxx> jupyter notebook

此时发现,运行环境还是原来的默认运行环境,并不是当前的 “pytorch-gpu”。

3. 手动添加 Jupyter 的kernel

使用ipykernel来管理多个kernel

历史:Jupyter原名叫ipython notebook,之后拆分为独立工具,但只代表web UI的部分,背后其实依然依赖ipython来执行code。

所以要改动Jupyter的kernel,就代表需要改动ipython的kernel,而ipython提供了ipykernel的工具来管理kernel。

安装ipykernel

(pytorch-gpu) C:\Users\xxx> conda install ipykernel

安装完后,重启jupyter notebook就可以看见kernel的选项了,如下图。
Jupyter notebook 运行环境创建和切换 (Win10+Anaconda)
此时可能仍然没有你想要的kernel,那么我们手动创建。我们已经在创造的环境中安装了ipykernel了,所以,我们直接在环境里执行下面的代码来手动添加kernel进jupyter notebook

python -m ipykernel install --user --name [虚拟环境名] --display-name "kernel命名"

例如

python -m ipykernel install --user --name pytorch-gpu --display-name "Python(pytorch-gpu)"

此时,就可以看到创建的Python(pytorch-gpu)了,选择该kernel运行即可。

参考:
Jupyter notebok 环境配置,与kernel切换(切换虚拟环境)
https://zhuanlan.zhihu.com/p/50456606

在Jupyter同時使用Python2 + Python3
https://carsonwah.github.io/15213187969431.html