How to clone an existing conda environment in python ?


Introduction

If you are a Python user, you might have encountered situations where you need to recreate an environment that already exists. This is where the concept of cloning an existing conda environment comes in.

Prior to installing new libraries with "conda install," I prefer to backup my current one in case any issues arise. This way, I can easily go back to my previous environment without having to reinstall everything. In this section, you will learn how to clone an existing conda environment in python.

To begin with, let's quickly recap what a conda environment is. Essentially, a conda environment is a virtual workspace that contains specific versions of packages and their dependencies. This allows you to have multiple environments with different versions of packages on the same system, which is especially useful when working on multiple projects.

For instance, I have a dedicated environment for web development using Django, and another work environment where I focus on other projects.

Cloning a Conda Environment

To begin, start by listing all of your existing Conda environments. Open your terminal and run the following command:

conda env list

This command will provide you with a comprehensive list of all your Conda environments, including their respective paths.

As an illustration, in my particular scenario, I received the following results:

(worklab) bmarchant@BenjamiacStudio 2019_new % conda env list
# conda environments:
#
                         /Users/bmarchant/miniforge3
                         /Users/bmarchant/miniforge3/envs/mlp
base                  /Users/bmarchant/opt/anaconda3
mlp                   /Users/bmarchant/opt/anaconda3/envs/mlp
websitedev        /Users/bmarchant/opt/anaconda3/envs/websitedev
worklab          * /Users/bmarchant/opt/anaconda3/envs/worklab

Now, identify the specific environment that you wish to clone. For the purpose of this guide, let's assume you want to clone an environment called "existing_environment_name".

To clone an existing conda environment, use the following command:

conda create --name <new_environment_name> --clone <existing_environment_name>

In this case, we are using the "conda create" command with the --clone option, followed by the name of the existing environment and the desired name for the new environment. Once you hit enter, conda will start cloning the environment and install all the packages and dependencies.

In my case, I needed to clone my worklab environment, so I executed the following command:

conda create --name worklab_clone --clone worklab

Upon inspecting the conda env list, I can now confirm the successful creation of the new environment:

(worklab) bmarchant@BenjamiacStudio 2019_new % conda env list                                   
# conda environments:
#
                         /Users/bmarchant/miniforge3
                         /Users/bmarchant/miniforge3/envs/mlp
base                  /Users/bmarchant/opt/anaconda3
mlp                   /Users/bmarchant/opt/anaconda3/envs/mlp
websitedev        /Users/bmarchant/opt/anaconda3/envs/websitedev
worklab         *  /Users/bmarchant/opt/anaconda3/envs/worklab
worklab_clone    /Users/bmarchant/opt/anaconda3/envs/worklab_clone

Please note that the asterisk in the conda env list indicates the currently active environment.

After completion, you can activate your new environment using:

conda activate <new_environment_name>

or

source activate <new_environment_name>

Why Clone a Conda Environment?

Now, you might be wondering why cloning an environment is necessary when we can simply create a new environment and install the required packages. Well, here are some reasons:

  • Replicability: By cloning an existing conda environment, you ensure that other users or systems have the exact same setup as yours. This makes it easier to share code and collaborate on projects.
  • Time-saving: Cloning an environment is much faster than installing all the packages and dependencies from scratch. This can save you a lot of time, especially when working on multiple projects with similar requirements.
  • Dependency management: When you clone an environment, you also clone its dependencies. This ensures that your code always runs smoothly without worrying about package conflicts or compatibility issues.

References

Links Site
Managing environments conda.io