Moving Conda Environments

Conda is known as a package manager for Python and R packages produced by Anaconda, Inc. and conda-forge, the open-source community for conda Python packages. In addition to managing packages, Conda is also an environment manager. If you’re new to Python, environments create an isolated environment to manage dependencies in a project. Because the Python ecosystem of packages is both wide and deep, part of Conda’s job is to install packages that don’t conflict with each other.

Once you have your project environment set up and your code written and tested, you may want to move it to another machine. For example, you would want to move a web application to server with a public address or copy a set of tools you frequently use to a USB drive. You might want to take a snapshot of your project environment as a backup.

Conda provides a number of ways to preserve and move environments.

Clone

On installation, conda creates a base environment. However, you can also create your own base environment with packages you frequently use. The – -clone option will create a clone (or snapshot) of the environment,

conda create --name snapshot --clone myenv

Spec List

If you need to reproduce an environment across computers with the same operating system, you can generate a spec list. For example, if you’re teaching a course and need to copy the environment for course exercises, the – -explicit option will produce a list of packages. To write the environment specifications to a file:

conda list --explicit spec-list.txt

To recreate the environment:

conda create --name python-course --file spec-list.txt

Environment.yml

Sharing a project environment across platforms and operating systems can also be done using the – -export option to generate an environment.yml file. The difference between a spec list and an environment.yml file is that the environment.yml file is not operating system specific and is formatted using YAML. Only the package names are listed and conda is left to build the environment based on the package names. Another difference is that – -export also includes packages installed using pip whereas a spec list does not. To export an environment.yml file:

conda env export environment.yml

* Note that if you have an existing environment.yml file in the path, conda will overwrite that file. To create an environment:

conda env create -f environment.yml

Conda Pack

Conda-pack is a command line tool that archives a conda environment, which includes all the binaries of the packages installed in the environment. This is useful when you want to reproduce an environment with limited or no internet access. All the previous methods download packages from their respective repositories to create an environment. Keep in mind that conda-pack is both platform and operating system specific and that the target computer must have the same platform and OS as the source computer.

To install conda-pack, make sure you are in the root or base environment so that it is available in sub-environments. Conda-pack is available at conda-forge or PyPI.

conda-forge:

conda install -c conda-forge conda-pack

PyPI:

pip install conda-pack

To package an 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
 

To install the environment:

 
 
# 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
 

Moving, Moving, Moving

Conda provides multiple ways of reproducing project environments. Creating a clone of an environment can provide a custom base environment or snapshot of the environment. Spec list and conda-pack create platform and operating system specific copies of an environment. Conda will use a spec list to download the exact packages in an environment. Alternatively, conda-pack archives an entire environment including the package binaries, which is useful in low or no bandwidth situations. Exporting an environment using conda is ideal for recreating environments across different platforms and operating systems.

Read more about environments at docs.conda.io and conda-pack on the project page.

Talk to an Expert

Talk to one of our experts to find solutions for your AI journey.

Talk to an Expert