Conda Environment Transfer

Made by Mike_Zhang


Unfold Linux Topics $\triangledown$


Intro

配置Conda环境常常是头疼的一个过程。更头疼的是,当你需要在不同的机器上配置相同的环境时,你需要重新安装这好不容易配置好的环境。本文将介绍如何将Conda环境无痛迁移到另一台机器上。

1. Conda Environment Transfer

1.1 Method 1 - Transfer the Entire Environment Folder

1.1.1 Step1: Pack the Environment

On source machine:

1
2
3
4
5
6
7
8
# 1. [Optional] Go to the home directory
cd ~

# 2. Activate the environment you want to transfer
conda activate my_env

# 3. Pack the environment
conda pack -n my_env -o my_env.tar.gz

1.1.1.1 Error Handling: Cannot pack an environment with editable packages

Error:

1
2
3
CondaPackError: Cannot pack an environment with editable packages
installed (e.g. from `python setup.py develop` or
`pip install -e`). Editable packages found:

Solution: Add --ignore-editable-packages

1
conda pack -n my_env -o my_env.tar.gz --ignore-editable-packages

1.1.1.2 Error Handling: Files managed by conda were found to have been deleted/overwritten

Error:

1
2
3
CondaPackError: 
Files managed by conda were found to have been deleted/overwritten in the
following packages:

Solution: Add --ignore-missing-files

1
conda pack -n my_env -o my_env.tar.gz --ignore-missing-files

1.1.2 Step2: Transfer the Packed Environment

Move the my_env.tar.gz to the /home/xxx/anaconda3/envs on the target machine

  • Where /home/xxx/anaconda3/envs is the path to the Conda envs folder on the target machine. Modify it according to your machine.

1.1.3 Step3: Unpack and Activate the Environment

On the target machine:

1
2
3
4
5
6
7
8
9
10
11
# 1. Go to the envs folder
cd /home/xxx/anaconda3/envs

# 2. Create a new folder for the env
mkdir -p my_env

# 3. Unpack the environment
tar -xzf my_env.tar.gz -C my_env

# 4. Activate the environment
conda activate my_env

1.2 Method 2 - Export and Import Environment via file

1.2.1 Step1: Export the Environment

On the source machine:

1
2
3
4
5
6
7
8
# 1.[Optional] Go to the home directory
cd ~

# 2. Activate the environment you want to transfer
conda activate my_env

# 3. Export the environment
conda env export > my_env_environment.yml

1.2.2 Step2: Transfer the Environment File

Move the my_env_environment.yml to the target machine

1.2.3 Step3: Create and Activate the Environment

On the target machine:

1
2
3
4
5
6
7
8
# 1. Go to the folder where the environment file is
cd path/to/my_env_environment.yml

# 2. Create the environment
conda env create -f my_env_environment.yml

# 3. Activate the environment
conda activate my_env

References

Csdn.net, 2024. https://blog.csdn.net/weixin_44151034/article/details/139757268
Csdn.net, 2024. ‌https://blog.csdn.net/maximejia/article/details/115385868
Segmentfault.com, 2024. https://segmentfault.com/a/1190000042192237


原创文章,转载请标明出处
Made by Mike_Zhang




感谢你的支持 | Thank you for supporting

Conda Environment Transfer
https://ultrafish.io/post/conda-env-transfer/
Author
Mike_Zhang
Posted on
December 27, 2024
Licensed under