Open-Source Internship opportunity by OpenGenus for programmers. Apply now.
In this article, we have demonstrated commands using which you can rename a Conda environment. It is not directly possible to rename a conda environment but there are work-arounds.
Table of contents:
- Step 1: Find the Conda environment to clone
- Step 2: Get out of the environment
- Step 3.1: Rename using Clone command
- Step 3.2: Rename by moving files
- Why rename 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.1: Rename using Clone command
One approach is to:
- Clone the old environment to a new environment with the name we want
- Delete the old environment
The commands are:
conda create --name new_env --clone old_env
conda remove --name old_env --all
To find other ways to clone an environment, follow the commands in this article.
To find other ways to delete an environment, follow the commands in this article.
Step 3.2: Rename by moving files
This is an alternative to the previous command / step 3.1.
In this case, we want to rename an environment (OLD_ENV) to the new name NEW_ENV by:
- Creating a new environment named
new_name
- Copy files manually from the old environment
old_name
to the location of new environment - Fix the references to old name within the files
- Delete the old environment and the new environment is the renamed version.
Create variable storing the new and old names in the console:
OLD_ENV = "old_name"
NEW_ENV = "new_name"
- Create an environment by copying the files
cp -r $OLD_ENV $NEW_ENV
mv $OLD_ENV $NEW_ENV
Change the references to old name in the files:
sed -i.bak "s:envs/$OLD_ENV/bin:envs/$NEW_ENV/bin:" $NEW_ENV/bin/*
- Remove backups in new environment
rm $NEW_ENV/bin/*.bak
To revert the above changes (in case things go wrong), use the following commands:
mv $NEW_ENV $OLD_ENV
$ sed -i.bak "s:envs/$NEW_ENV/bin:envs/$OLD_ENV/bin:" $OLD_ENV/bin/*
Why rename a Conda environment?
You may need to rename a Conda Environment for the following reasons:
- Save environments: Ocassionally, conda environments are created by scripts to a specific name. This removes or overwrites the original environment. To save the original environment, it is wise to rename the conda environment.
- Good practice: It is a good practice to name things with a descriptive name so is the case with conda environments.
With this article at OpenGenus, you must have the complete idea of how to rename a Conda environment.