Replication and Migration of Conda Virtual Environments

Replication and Migration of Conda Virtual Environments

Copy the Conda virtual environment locally

conda create --name 新环境名 --clone 旧环境名

Duplicate environments between the same operating systems

Method 1: requirements.txt

This method is not recommended, because it will only export the dependent packages you installed using pip, and will not export the packages that the virtual environment depends on, and is not suitable for the application scenario of virtual environment migration.

In fact, this method is more suitable for situations where we already know which packages we depend on and we only need package information, such as writing project documents and telling others which dependent packages must be installed to run my system. And ignore the dependent environment of the virtual environment itself.

pip freeze > requirements.txt  # 生成requirements.txt
pip install -r requirements.txt  # 从requirements.txt安装依赖

Method 2: Environment.yml

Sharing project environments across platforms and operating systems can also be done using the -export option to generate an environment.yml file. The difference between the canonical list and the environment.yml file is that the environment.yml file is not OS-specific, but is formatted using YAML. Only the package name is listed, and conda builds the environment according to the package name. Another difference is that -export also includes packages installed with pip, while the spec list does not. To export the environment.yml file:

conda env export environment.yml
  • Note that if an environment.yml file already exists in the path, conda will overwrite it. Create environment:
conda env create -f environment.yml

Method 3: Conda Pack

Conda-pack is a command-line tool for packaging conda environments, which includes all binaries for packages installed in the environment. This method supports the use in disconnected environment. Environment.yml is to download packages from the respective repositories of python dependent packages to create an environment. Of course, the disadvantages are slow network speed, easy download failure and so on. This method does not have this problem.

Remember that conda-pack is platform and OS specific, and the target machine must have the same platform and OS as the source machine.
To install conda-pack, make sure you are in the root or base environment so that it is available in subenvironments. Conda-pack is available at conda-forge or PyPI.

conda-forge:

conda install -c conda-forge conda-pack

PyPI:

pip install conda-pack

Packaging environment:

# Pack environment my_env into my_env.tar.gz
$ conda pack -n my_env

# Pack environment my_env into out_name.tar.gz
$ conda pack -n my_env -o out_name.tar.gz

# Pack environment located at an explicit path into my_env.tar.gz
$ conda pack -p /explicit/path/to/my_env

Copy the environment to other computers:

# Unpack environment into directory `my_env`
$ mkdir -p my_env
$ tar -xzf my_env.tar.gz -C my_env

# Use Python without activating or fixing the prefixes. Most Python
# libraries will work fine, but things that require prefix cleanups
# will fail.
$ ./my_env/bin/python

# Activate the environment. This adds `my_env/bin` to your path
$ source my_env/bin/activate

# Run Python from in the environment
(my_env) $ python

# Cleanup prefixes from in the active environment.
# Note that this command can also be run without activating the environment
# as long as some version of Python is already installed on the machine.
(my_env) $ conda-unpack

Method 4: Directly copy the virtual environment folder in the envs directory

In a disconnected environment, we can also directly copy the entire virtual environment from the source computer (hereinafter collectively referred to as the src computer) to the target computer (hereinafter collectively referred to as the target computer). The premise is that the target computer must have the same platform and operating system as the src computer.

1. Install the same version of Anaconda on the target computer.

2. From the src computer, copy the virtual environment folder that needs to be copied in the envs folder to the target computer, and place it anywhere.

We can use the following command to view the storage path of the virtual environment folder on the src computer:

conda env list

image-20230426143639701

Take the above figure as an example, E:\ProgramData\Anaconda\envs\new_name is the storage path of the virtual environment new_name. We can copy it directly to the target computer.

image-20230426143959988

3. On the target computer, enter the conda command line and use the following command:

conda config --add envs_dirs %复制到target电脑上的envs路径%

Note: The path here is the parent directory of the target virtual environment folder!

for example:

I copied the new_name in the above picture to the target computer and renamed it: env_pybd. The path is G:\anoconda_envs\env_pybd

image-20230426152249543

Then I should execute the command with conda:

conda config --add envs_dirs G:\anoconda_envs\

After running successfully, an envs_dirs record will be added to C:\user%current username%\.condarc, as shown in the following figure:

image-20230426152437166

run again

conda env list

You can see that we have copied successfully:

image-20230426152756755

Guess you like

Origin blog.csdn.net/guigenyi/article/details/130404653