> For the complete documentation index, see [llms.txt](https://kerasnoone.gitbook.io/garnet/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://kerasnoone.gitbook.io/garnet/gong-cheng-zhan/jupyter/jupyter-duo-huan-jing-pei-zhi.md).

# jupyter多环境配置

无论是使用Jupyter Notebook还是Jupyter Lab, 在实际研发过程中, 在多个版本的开发环境之间切换, 这种场景还是经常遇到的. 无论是前些年Python2和Python3之间的切换, 现在在稳定的Python3.6和最新的Python版本之间切换, 亦或是由于不同版本的tensorflow和pytorch之间切换, 都是在开发过程中会遇到的问题.

模型的调参训练过程, 基本都会使用Jupyter工具进行, 在Jupyter中配置多环境, 就是一个必须要掌握的能力.

## 虚拟环境

多环境指的就是多个虚拟环境. 在Python中, 可以使用`virtualenv`和`conda`工具创建并管理虚拟环境. Jupyter依赖于conda环境, 因此像Jupyter中添加多个环境, 就是添加新的conda虚拟环境.

## conda虚拟环境

因此第一步就是要创建conda虚拟环境.

### 查看conda环境列表

可以使用`conda env list`查看当前已有的虚拟环境. 通过Anaconda安装, 就会有一个`conda-env`环境.

```
garnet@DESKTOP-DH5TCF8:~$ conda list env
# packages in environment at /home/garnet/env/anaconda3:
#
# Name                    Version                   Build  Channel
conda-env                 2.6.0                h36134e3_1
```

### 创建虚拟环境

创建虚拟环境使用指令:

```
conda create -n your_env_name python=x.x
```

这里的`your_env_name`是新虚拟环境的名字, `python=x.x`制定python版本, 如果不指定则使用系统默认的python版本.

要注意的是, **创建conda虚拟环境是要联网进行的**, 因为需要从源仓库中下载. 但实际的测试环境和生产环境往往是不连接外网的, 一般公司内部也没有conda源. 因此安装的时候需要**离线安装**. 离线安装有两种方法:

第一种是在后面添加`--offline`参数:

```
conda create -n your_env_name --offline
```

另一种是指定一个已有的环境, 直接复制:

```
conda create -n new_env_name --clone raw_env_name
```

`new_env_name`是新的虚拟环境的名字, `raw_env_name`是被复制的环境的名称.

```
garnet@DESKTOP-DH5TCF8:~$ conda create -n py3.6-tf1.14-cuda10.0 --offline
Solving environment: done

## Package Plan ##

  environment location: /home/garnet/env/anaconda3/envs/py3.6-tf1.14-cuda10.0


Proceed ([y]/n)? y

Preparing transaction: done
Verifying transaction: done
Executing transaction: done
#
# To activate this environment, use:
# > source activate py3.6-tf1.14-cuda10.0
#
# To deactivate an active environment, use:
# > source deactivate
```

再看下现在的conda环境列表:

```
garnet@DESKTOP-DH5TCF8:~$ conda env list
# conda environments:
#
base                  *  /home/garnet/env/anaconda3
py3.6-tf1.14-cuda10.0     /home/garnet/env/anaconda3/envs/py3.6-tf1.14-cuda10.0
```

### 进入虚拟环境

在linux环境中使用以下的指令进入到指定的虚拟环境中:

```
source activate your_env_name
```

进入后退出使用以下指令:

```
source deactivate
```

windows的指令则为:

```
activate your_env_name
deactivate
```

现在进入到刚刚创建的虚拟环境中:

```
garnet@DESKTOP-DH5TCF8:~$ source activate py3.6-tf1.14-cuda10.0
(py3.6-tf1.14-cuda10.0) garnet@DESKTOP-DH5TCF8:~$
```

### 删除虚拟环境

如果一个虚拟环境不用了, 为了节省空间删除, 使用以下指令:

```
conda remove -n your_env_name --all
```

## jupyter安装conda环境

在启动jupyter实验平台的环境中安装`ipykernel`包, 这个环境一般是原始的conda环境, 即一般在这一步不需要进入到虚拟环境:

```
pip install ipykernel
```

然后执行下面的指令, 就可以将新建的conda环境安装到jupyter环境中, 成为一个可选的环境:

```
python -m ipykernel install --name conda_env_name --display-name "jupyter_display_name"
```

其中`conda_env_name`为conda环境的名字, `jupyter_display_name`为在jupyter中的展示名称, 如果没有指定, 则与conda\_env\_name保持一致.

在安装中可能遇到没有权限的问题, 只需要添加`--user`参数即可.

```
(py3.6-tf1.14-cuda10.0) garnet@DESKTOP-DH5TCF8:~/.pip$ python -m ipykernel install --name py3.6-tf1.14-cuda10.0 --display-name tf1.14-cuda10.0 --user
Installed kernelspec py3.6-tf1.14-cuda10.0 in /home/garnet/.local/share/jupyter/kernels/py3.6-tf1.14-cuda10.0
```

看到上面的显示, 我们就成功地将新的虚拟环境安装到了jupyter中.

可以使用`jupyter kernelspec list`指令查看当前jupyter所有运行的conda环境.

```
(py3.6-tf1.14-cuda10.0) garnet@DESKTOP-DH5TCF8:~/.pip$ jupyter kernelspec list
Available kernels:
  py3.6-tf1.14-cuda10.0    /home/garnet/.local/share/jupyter/kernels/py3.6-tf1.14-cuda10.0
  python3                  /home/garnet/env/anaconda3/share/jupyter/kernels/python3
```

可以看到当前jupyter中包含了刚刚安装的conda环境.

### jupyter卸载conda环境

如果一个conda环境我们不再使用了, 也可以将这个环境从jupyter中卸载, 使用以下的指令:

```
jupyter kernelspec remove kernel_name
```

`kernel_name`指的是对应的conda环境的名字, 而不是在jupyter中展示的名字.

## 启动jupyter

参考[jupyter实验环境启动](/garnet/gong-cheng-zhan/jupyter/jupyter-shi-yan-huan-jing-qi-dong.md).

可以看到在Jupyter Lab环境中有了我们刚才创建并加载到jupyter中的conda环境.

![](/files/-MWALDYVfJHsvZl8paNH)
