Open-Source Internship opportunity by OpenGenus for programmers. Apply now.
In this article, we have presented commands to clone a Conda environment that is to create a duplicate conda environment with a new name. There are multiple options like using clone command, update command or copy files directly.
Table of contents:
- Step 1: Find the Conda environment to clone
- Step 2: Get out of the environment
- Step 3: Clone the Conda Environment
- Alternative to Step 3: Clone the Conda Environment using update
- Copy Directory directly?
- Why to clone a Conda Environment?
Step 1: Find the Conda environment to clone
To find the name of the environment you want to clone, we can get the list of all Conda environments as follows:
conda env list
Let the name of the environment to be delete is corrupted_env
.
Step 2: Get out of the environment
To clone a conda environment, we shall get out of it that is it should not be our current conda environment. To get out of the current environment, use the command:
conda deactivate
Step 3: Clone the Conda Environment
The simplest command to clone is as follows:
conda create --name cloned_env --clone original_env
cloned_env
is the new conda environmentoriginal_env
is the conda environment we are cloning / copying
Alternative to Step 3: Clone the Conda Environment using update
A second option is to save the details of the original environment in a file and use the file to create a new conda environment.
- Go into original environment, save details and get out
conda activate original_env
conda env export > environment.yml
conda deactivate
- Create a new environment and go into it
conda create --name cloned_env
conda activate cloned_env
- Update the 2nd environment using the file
conda env update --name root --file environment.yml
- Get out of the 2nd conda environment:
conda deactivate cloned_env
After cloning the Conda environment, you may want to delete the original conda environment. To delete, you must go through commands in this article.
Copy Directory directly?
It is not advised to copy the directory directly where the conda environment is stored. In some cases, it might be necessary so the steps are:
- Find the path of the conda environment using:
conda info --envs
- Delete the directory directly:
cp -r /Users/username/.local/share/conda/envs/previous_env location_of_new_env
Why to clone a Conda Environment?
You may need to clone a Conda Environment for the following reasons:
- Incremental Changes: You may need to make incremental changes on a Conda Environment but you also, want to preserve the current state of the environment. In this case, you shall clone the environment and work on the 2nd copy.
- Trying disruptive changes: If you need to try something which may corrupt the conda environment or if you need to share your environment with any user, it is wise to create a 2nd copy of the conda environment.
With this article at OpenGenus, you must have the complete idea of how to Clone Conda Environment.