Help Center> ModelArts> Best Practices> Notebook> Creating, Migrating, and Managing Conda Virtual Environments Based on SFS
Updated on 2024-01-09 GMT+08:00

Creating, Migrating, and Managing Conda Virtual Environments Based on SFS

This topic describes how to migrate the Conda environment on a notebook instance to an SFS disk. In this way, the Conda environment will not be lost after the notebook instance is restarted.

The procedure is as follows:

  1. Creating a Virtual Environment and Saving It to the SFS Directory
  2. Cloning the Existing Virtual Environments to the SFS Disk
  3. Restarting the Image to Activate the Virtual Environment in the SFS Disk
  4. Saving and Sharing the Virtual Environment

Prerequisites

You have created a notebook instance by setting Resource Type to Dedicated resource pool and Storage to SFS and opened the terminal.

Creating a Virtual Environment and Saving It to the SFS Directory

Create a conda virtual environment.

# shell
conda create --prefix /home/ma-user/work/envs/user_conda/sfs-new-env python=3.7.10 -y

View the existing conda virtual environments. The name of the newly created virtual environment may be empty in the output.

# 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

Append the new virtual environment to conda envs.

# shell
conda config --append envs_dirs /home/ma-user/work/envs/user_conda/

View the existing conda virtual environments. The new virtual environment is properly displayed, and you can switch to it by name.

# 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

(Optional) Register the new virtual environment with the JupyterLab kernel, so that you can directly use it in 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-*

Note: .local/share/jupyter/kernels/sfs-new-env is used as an example only. Replace it with the actual installation path.

Refresh the JupyterLab page. The new kernel is displayed.

After the notebook instance is restarted, the kernel needs to be registered again.

Cloning the Existing Virtual Environments to the SFS Disk

# 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

View the cloned virtual environments. If the name of the newly created virtual environment is empty, handle the issue according to Append the new virtual environment to conda envs.

# 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

(Optional) Register the new virtual environment with the JupyterLab kernel, so that you can directly use it in 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-*

Note: .local/share/jupyter/kernels/sfs-clone-env is used as an example only. Replace it with the actual installation path.

Refresh the JupyterLab page. The new kernel is displayed.

Restarting the Image to Activate the Virtual Environment in the SFS Disk

Method 1: Use the complete conda env path.

# shell
conda activate /home/ma-user/work/envs/user_conda/sfs-new-env

Method 2: Append the virtual environment to conda envs and activate it using its name.

# shell
conda config --append envs_dirs /home/ma-user/work/envs/user_conda/
conda activate sfs-new-env

Method 3: Use Python or pip in the virtual environment.

# 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

Saving and Sharing the Virtual Environment

Package the virtual environment to be migrated.

# 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

Decompress the package to the SFS directory.

# 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

View the existing conda virtual environments.

# 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