
# 将Notebook的Conda环境迁移到SFS磁盘
本文介绍了如何将Notebook的Conda环境迁移到SFS磁盘上。这样重启Notebook实例后，Conda环境不会丢失。
步骤如下:
1. [创建新的虚拟环境并保存到SFS目录]
2. [克隆原有的虚拟环境到SFS盘]
3. [重新启动镜像激活SFS盘中的虚拟环境]
4. [保存并共享虚拟环境]
#### 前提条件
创建一个Notebook，"资源类型"选择"专属资源池"，"存储配置"选择"SFS弹性文件服务器"，打开terminal。
 #### 创建新的虚拟环境并保存到SFS目录
创建新的conda虚拟环境。
```
# shell
conda create --prefix /home/ma-user/work/envs/user_conda/sfs-new-env python=3.7.10 -y
```
查看现有的conda虚拟环境，此时可能出现新创建的虚拟环境的名称为空的情况。
```
# shell
conda env list
```
```
# conda environments:
#
base                     /home/ma-user/anaconda3
PyTorch-1.8              /home/ma-user/anaconda3/envs/PyTorch-1.8
python-3.7.10         *  /home/ma-user/anaconda3/envs/python-3.7.10
                         /home/ma-user/work/envs/user_conda/sfs-new-env
```
添加新创建的虚拟环境到conda env。
```
# shell
conda config --append envs_dirs /home/ma-user/work/envs/user_conda/
```
查看现有的conda虚拟环境，此时新的虚拟环境已经能够正常显示，可以直接通过名称进行虚拟环境的切换。
```
# shell
conda env list
conda activate sfs-new-env
```
```
# conda environments:
#
base                     /home/ma-user/anaconda3
PyTorch-1.8              /home/ma-user/anaconda3/envs/PyTorch-1.8
python-3.7.10         *  /home/ma-user/anaconda3/envs/python-3.7.10
sfs-new-env              /home/ma-user/work/envs/user_conda/sfs-new-env
```
（可选）将新建的虚拟环境注册到JupyterLab kernel（可以在JupyterLab中直接使用虚拟环境）。
```
# shell
pip install ipykernel
ipython kernel install --user --name=sfs-new-env
rm -rf /home/ma-user/.local/share/jupyter/kernels/sfs-new-env/logo-*
```
说明：此处".local/share/jupyter/kernels/sfs-new-env"为举例，请以用户实际的安装路径为准。
图1安装路径回显   
![](https://support.huaweicloud.com/bestpractice-modelarts/figure/zh-cn_image_0000001490709721.png "点击放大")
刷新JupyterLab页面，可以看到新的kernel。
![](https://support.huaweicloud.com/bestpractice-modelarts/public_sys-resources/note_3.0-zh-cn.png)
重启Notebook后kernel需要重新注册。
 #### 克隆原有的虚拟环境到SFS盘
```
# shell
conda create --prefix /home/ma-user/work/envs/user_conda/sfs-clone-env --clone PyTorch-1.8 -y
```
```
Source:      /home/ma-user/anaconda3/envs/PyTorch-1.8
Destination: /home/ma-user/work/envs/user_conda/sfs-clone-env
Packages: 20
Files: 39687
Preparing transaction: done
Verifying transaction: done
Executing transaction: done
#
# To activate this environment, use
#
#     $ conda activate /home/ma-user/work/envs/user_conda/sfs-clone-env
#
# To deactivate an active environment, use
#
#     $ conda deactivate
```
查看新创建的clone虚拟环境，如果出现新创建的虚拟环境的名称为空的情况，可以参考[添加新创建到虚拟环境到conda env]。
```
# shell
conda env list
```
```
# conda environments:
#
base                     /home/ma-user/anaconda3
PyTorch-1.8              /home/ma-user/anaconda3/envs/PyTorch-1.8
python-3.7.10            /home/ma-user/anaconda3/envs/python-3.7.10
sfs-clone-env            /home/ma-user/work/envs/user_conda/sfs-clone-env
sfs-new-env           *  /home/ma-user/work/envs/user_conda/sfs-new-env
```
（可选）将新建的虚拟环境注册到JupyterLab kernel（可以在JupyterLab中直接使用虚拟环境）
```
# shell
pip install ipykernel
ipython kernel install --user --name=sfs-clone-env
rm -rf /home/ma-user/.local/share/jupyter/kernels/sfs-clone-env/logo-*
```
说明：此处".local/share/jupyter/kernels/sfs-clone-env"为举例，请以用户实际的安装路径为准。
刷新JupyterLab页面，可以看到新的kernel。
 #### 重新启动镜像激活SFS盘中的虚拟环境
方法一，直接使用完整conda env路径。
```
# shell
conda activate /home/ma-user/work/envs/user_conda/sfs-new-env
```
方法二，先添加虚拟环境到conda env，然后使用名称激活。
```
# shell
conda config --append envs_dirs /home/ma-user/work/envs/user_conda/
conda activate sfs-new-env
```
方法三，直接使用完成虚拟环境中的python或者pip。
```
# shell
/home/ma-user/work/envs/user_conda/sfs-new-env/bin/pip list
/home/ma-user/work/envs/user_conda/sfs-new-env/bin/python -V
```
 #### 保存并共享虚拟环境
将要迁移的虚拟环境打包。
```
# shell
pip install conda-pack
conda pack -n sfs-clone-env -o sfs-clone-env.tar.gz --ignore-editable-packages
```
```
Collecting packages...
Packing environment at '/home/ma-user/work/envs/user_conda/sfs-clone-env' to 'sfs-clone-env.tar.gz'
[########################################] | 100% Completed |  3min 33.9s
```
解压到SFS目录。
```
# shell
mkdir /home/ma-user/work/envs/user_conda/sfs-tar-env
tar -zxvf sfs-clone-env.tar.gz -C /home/ma-user/work/envs/user_conda/sfs-tar-env
```
查看现有的conda虚拟环境。
```
# shell
conda env list
```
```
# conda environments:
#
base                     /home/ma-user/anaconda3
PyTorch-1.8           *  /home/ma-user/anaconda3/envs/PyTorch-1.8
python-3.7.10            /home/ma-user/anaconda3/envs/python-3.7.10
sfs-clone-env            /home/ma-user/work/envs/user_conda/sfs-clone-env
sfs-new-env              /home/ma-user/work/envs/user_conda/sfs-new-env
sfs-tar-env              /home/ma-user/work/envs/user_conda/sfs-tar-env
test-env                 /home/ma-user/work/envs/user_conda/test-env
```
