pkowalczyk
pkowalczyk

Reputation: 18483

How can I rename a conda environment?

I have a conda environment named old_name, how can I change its name to new_name without breaking references?

Upvotes: 633

Views: 464971

Answers (10)

Long
Long

Reputation: 1795

Update: You can now officially rename a conda environment, but THIS IS SLOWER

conda rename -n old_env_name new_env_name

This command clone the new environment and performs downloading and installation

Logs:

❯ conda rename -n a b
Source:      /home/long/work/miniconda3/envs/a
Destination: /home/long/work/miniconda3/envs/b
Packages: 20
Files: 61608

Downloading and Extracting Packages:


Downloading and Extracting Packages:

Preparing transaction: done
Verifying transaction: done
Executing transaction: done

INSTEAD, TRY THE ONE BELOW, IT'S FASTER & SAFER

  1. Activate & Export your current environment conda env export > environment.yml
  2. Deactivate current conda environment. Modify the environment.yml file and change the name of the environment as you like (or check step 3 for --name argument), also you should delete the prefix line as well.
  3. Create a new conda environment by executing this conda env create -f environment.yml. Note that if you want to change/overwrite the name of the new environment you plan to create, add --name new_env_name as follows:
conda env create --name new_env_name -f environment.yml

I suggest an even safer way, that is using conda update

conda env update --name your_env_name --file environment.yml --prune

because update will update the current (new) environment if it's already created, or create a new environment if it has not been, and --prune will make sure that any dependencies that are not listed in the environment.yml file are removed from the environment.

This process takes a couple of minutes, and now you can safely delete the old environment.

Logs:

❯ conda env export > environment.yml
❯ conda deactivate 
❯ conda env create --name b -f environment.yml
Retrieving notices: ...working... done
Channels:
 - defaults
Platform: linux-64
Collecting package metadata (repodata.json): done
Solving environment: done

Downloading and Extracting Packages:

Preparing transaction: done
Verifying transaction: done
Executing transaction: done

Upvotes: 12

Matt Dancho
Matt Dancho

Reputation: 7298

Simply rename the environment's folder

This was the easiest solution to rename a conda environment on Windows. It likely works on Mac too (just haven't tested it). I just renamed my old environment directory and it worked:

mv ~/anaconda3/envs/old_name ~/anaconda3/envs/new_name

Source: https://github.com/conda/conda/issues/3097#issuecomment-314527753

Upvotes: 5

TG Gowda
TG Gowda

Reputation: 11957

conda should have given us a simple tool like cond env rename <old> <new> but it hasn't. Simply renaming the directory, as in this previous answer breaks the hardcoded hashbangs(#!). Hence, we need to go one more level deeper to get what we want.

conda env list
# conda environments:
#
base                  *  /home/tgowda/miniconda3
rtg                      /home/tgowda/miniconda3/envs/rtg

Here I am trying to rename rtg --> unsup (please bear with those names, this is my real use case)

$ cd /home/tgowda/miniconda3/envs 
$ OLD=rtg
$ NEW=unsup
$ mv $OLD $NEW   # rename dir

$ conda env list
# conda environments:
#
base                  *  /home/tgowda/miniconda3
unsup                    /home/tgowda/miniconda3/envs/unsup


$ conda activate $NEW
$ which python
  /home/tgowda/miniconda3/envs/unsup/bin/python

the previous answer reported upto this, but wait, we are not done yet! the pending task is, $NEW/bin dir has a bunch of executable scripts with hashbangs (#!) pointing to the $OLD env paths.

See jupyter, for example:

$ which jupyter
/home/tgowda/miniconda3/envs/unsup/bin/jupyter

$ head -1 $(which jupyter) # its hashbang is still looking at old
#!/home/tgowda/miniconda3/envs/rtg/bin/python

So, we can easily fix it with a sed

$ sed  -i.bak "s:envs/$OLD/bin:envs/$NEW/bin:" $NEW/bin/*  
# `-i.bak` created backups, to be safe

$ head -1 $(which jupyter) # check if updated
#!/home/tgowda/miniconda3/envs/unsup/bin/python
$ jupyter --version # check if it works
jupyter core     : 4.6.3
jupyter-notebook : 6.0.3

$ rm $NEW/bin/*.bak  # remove backups

Now we are done 💯

I think it should be trivial to write a portable script to do all those and bind it to conda env rename old new.


I tested this on ubuntu. For whatever unforseen reasons, if things break and you wish to revert the above changes:

$ mv $NEW  $OLD
$ sed  -i.bak "s:envs/$NEW/bin:envs/$OLD/bin:" $OLD/bin/*

Upvotes: 19

pkowalczyk
pkowalczyk

Reputation: 18483

New answer:

From Conda 4.14 you will be able to use just:

conda rename -n old_name  new_name 

Although, under the hood, conda rename still uses [1][2] undermentioned combination of conda create and conda remove.

Use the -d flag for dry-run (not destination, as of v22.11.0)

conda rename -n old_name -d new_name 

Old answer:

You can't.

One workaround is to create clone a new environment and then remove the original one.

First, remember to deactivate your current environment. You can do this with the commands:

  • deactivate on Windows or
  • source deactivate on macOS/Linux.

Then:

conda create --name new_name --clone old_name
conda remove --name old_name --all # or its alias: `conda env remove --name old_name`

Notice there are several drawbacks of this method:

  1. It redownloads packages (you can use --offline flag to disable it)
  2. Time consumed on copying environment's files
  3. Temporary double disk usage

There is an open issue requesting this feature.

Upvotes: 1008

Gon&#231;alo Peres
Gon&#231;alo Peres

Reputation: 13602

For that one can access the base/root environment and use conda rename.

Assuming one's environment is stack and one wants the name lab, one can do the following

conda rename -n stack lab

Other alternatives include

conda rename --name stack lab

conda rename -p path/to/stack lab

conda rename --prefix path/to/stack lab

Notes:

  • One cannot rename the base environment.

  • One cannot rename an active environment. If one is in the prompt of the environment stack one won't be able to do the operations above, and it will give a CondaEnvException

CondaEnvException: Cannot rename the active environment

  • If one tries to rename using an existing environment name, one will get a CondaEnvException. Using the case above, one will get

CondaEnvException: The environment 'lab' already exists. Override with --force.

Upvotes: 4

Sam Tseng
Sam Tseng

Reputation: 178

According to the answer of Thamme Gowda, the following steps work for me on my MacBook Pro:

  1. Change the folder name of the old env name into a new env name.
  2. Replace all the old env name in the shebang lines of all regular files under the bin folder in the new env folder.

The commands are:

$ conda deactivate
$ OLD=old_name
$ NEW=new_name
$ cd /Users/my_username/anaconda3/envs/
$ mv $OLD $NEW
$ find $NEW/bin/* -maxdepth 1 -type f -exec sed  -i.bak "s:envs/$OLD/bin:envs/$NEW/bin:" {} \;
$ conda activate new_name

Check if the shebang line is correctly replaced:

$ head -1 $(which jupyter) #!/Users/my_username/anaconda3/envs/new_name/bin/python

Upvotes: 2

Mihail Gusev
Mihail Gusev

Reputation: 79

I'm using Conda on Windows and this answer did not work for me. But I can suggest another solution:

  • rename enviroment folder (old_name to new_name)

  • open shell and activate env with custom folder:

    conda.bat activate "C:\Users\USER_NAME\Miniconda3\envs\new_name"

  • now you can use this enviroment, but it's not on the enviroment list. Update\install\remove any package to fix it. For example, update numpy:

    conda update numpy

  • after applying any action to package, the environment will show in env list. To check this, type:

    conda env list

Upvotes: 7

Bipin Maharjan
Bipin Maharjan

Reputation: 523

You can rename your Conda env by just renaming the env folder. Here is the proof:

Conda env renaming

You can find your Conda env folder inside of C:\ProgramData\Anaconda3\envs or you can enter conda env list to see the list of conda envs and its location.

Upvotes: -1

kkuma7
kkuma7

Reputation: 461

conda create --name new_name --copy --clone old_name is better

I use conda create --name new_name --clone old_name which is without --copy but encountered pip breaks...

the following url may help Installing tensorflow in cloned conda environment breaks conda environment it was cloned from

Upvotes: 46

bgoodr
bgoodr

Reputation: 2930

Based upon dwanderson's helpful comment, I was able to do this in a Bash one-liner:

conda create --name envpython2 --file <(conda list -n env1 -e )

My badly named env was "env1" and the new one I wish to clone from it is "envpython2".

Upvotes: 15

Related Questions