How can I rename a conda environment?
Asked Answered
M

10

599

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

Mite answered 14/2, 2017 at 16:51 Comment(2)
See also this post on how to clone a conda environmentJosselyn
you can't rename (frustrating!) but you can clone the old env with the new name and delete the old env: conda create --name new_name --clone old_name then delete old one: conda remove --name old_name --allPeroxide
M
952

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.

Mite answered 14/2, 2017 at 16:51 Comment(18)
so as to leave some work for those who fork conda and thereby making them understand much more en route :P :DHamil
@silgon Well, it's not my solution (but it's relatively obvious one). I just gathered this information on SO for better searchability - the github issue thread didn't showed up in google searches.Mite
Another way is to clone the environment conda create --name new_name --clone old_name then you can remove the old one.Artefact
Word of warning - I tried doing this, and ran into bizarre errors - my .bashrc no longer worked, and trying to run pytest would fail, trying to reference the now-removed environment. I tried new shells and restarting with no luck. Ended up having to blow away the new environment, then just start with a clean install. Slower, but seems to actually work now.Crunch
@CharlieParker -- as it turns out, there are a number of hard-coded paths that conda uses in environments. I think I was once able to successfully grep and sed my way through it, but it wasn't easy. If I can figure out out and make it consistent, perhaps I submit a PR.Unitive
@Crunch +5000 as I was going to try using this answer but instead I'm just going to pull the bandaid right off and recreate my badly named env from scratch.Exhalation
It takes quite long - I think you could update your answer to include --offline by default, and suggest omitting it if someone wants "freshly downloaded packages" at any cost (e.g. the source environment is somehow damaged). Maybe, if you don't mind wasting some disk space, you can keep the old environment around (I've done that actually).Course
Now on macOS (and presumably on linux), you can also simply use deactivate. (source deactivate is no longer necessary.)Refusal
You might want to add that it is a good idea to first activate old_name before cloning. I didn't do that and the clone included some incorrect versions from the current active environment.Mentality
Nice. But I got The following packages cannot be cloned out of the root environment:, which does not bode well, but as there's no other way, so be it!Dubitable
Maybe update the answer, issue is resolved with a new conda rename option. github.com/conda/conda/pull/11496Copulative
Worked perfectly for me, thank you. One additional note: The directory (and some "trash" files in it) of the old environment won't be removed from the file system with conda remove or conda env remove. This is another difference to the upcoming conda rename method.Engross
CommandNotFoundError: No command 'conda rename'Quartzite
The -d flag is for destination, not dry run!Fluid
If you are using conda straight after download, which is v4.12 for me, it don't have rename function, upgrade it first, mine was upgraded with long jump to v22.11.0 --- and if you happened to create the environment without name, you can use flag -p, for example conda rename -p c:\Users\john\python\env general-envIdle
Note that you need at least one conda environment active when you run this command - it will fail otherwise. Something as simple as conda activate base first, then conda rename -n old_name new_name will do the trick. github.com/conda/conda/issues/11850#issuecomment-1257215879Guido
I ran into TypeError: expected str, bytes or os.PathLike object, not NoneType. As per this github issue. some unrelated conda env need to be active when running conda rename command. I activated base env, ran the command and it worked.Positron
I just upgraded my conda installation to 4.14 and it says there's no command called conda renamePollypollyanna
H
46

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

Halloran answered 22/4, 2020 at 1:48 Comment(2)
mvenv () { conda create --name $2 --copy --clone $1 ; conda remove --name $1 --all ;}Delocalize
(Put that at the end of ~/.bashrc and you apparently have the requested feature. Feel free to incorporate it in your answer since I couldn't have conceived of your solution involving both --clone and --copy.)Delocalize
N
19

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/*
Nicolais answered 11/7, 2020 at 18:30 Comment(3)
If trivial it would be great for you to contribute this to the code base! Many would appreciate it (myself included)Southward
The sed -i '' "s/old/new" $NEW/bin/* will encounter "not a regular file" error, for me. To replace all the sheband lines in all regular files, I have to use: $ find $NEW/bin/* -maxdepth 1 -type f -exec sed -i.bak "s:envs/$OLD/bin:envs/$NEW/bin:" {} \; to escape the non-regular files.Goines
What about binary libraries that are relocated using rpath? It isn't simple to relocate conda environments because there is so much magic behind the scenes to make it work like an isolated environment.Liquefacient
E
16

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".

Exhalation answered 2/2, 2019 at 21:6 Comment(1)
Ran into an error saying PackagesNotFoundError: The following packages are not available from current channels with this script. This script probably works only when you don't have packages that are installed with pip and that are not available in conda in the existing environment?Verina
M
9

As the answer from @pkowalczyk mentioned some drawbacks: In my humble opinion, the painless and risk-free (workaround) way is following these steps instead:

  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 desire (usually it is on the first line of the yaml file)
  3. Create a new conda environment by executing this conda env create -f environment.yml

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

P.S. nearly 5 years and conda still does not have its "rename" functionality.

Macedonia answered 17/7, 2021 at 17:35 Comment(4)
I'm liking this answer, and in the middle of executing it. I believe, though, that the prefix: ... line should also be changed to the new name, so the env folder isn't re-used...?Chemosphere
** UPDATE ** - changing the prefix in environment.yml was NOT honored. But adding the -p option worked fine: conda env create -f environment.yml -p /path/to/envChemosphere
You don't have to change anything, instead, you provide --name while creating new environment. Note that your environment file should end with .yml, not .txt.Macedonia
Make sure that you change both the first line (name: <new name> ) and the final line (prefix: /dir1/dir2/../dirn/<new name>).Preciousprecipice
H
7

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

Hilburn answered 18/8, 2020 at 9:25 Comment(1)
Worked. Immediately showed in conda env list. Thanks.Magnetostriction
P
4

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.

Piliform answered 17/10, 2022 at 13:34 Comment(0)
C
3

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

Claudette answered 25/7, 2023 at 13:25 Comment(0)
G
2

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

Goines answered 9/3, 2022 at 16:53 Comment(0)
G
-1

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.

Groan answered 10/8, 2020 at 12:3 Comment(4)
That is not true for LinuxMagness
Bad idea: https://mcmap.net/q/64250/-how-can-i-rename-a-conda-environment explains whySuanne
recipe for disaster, don't do that!Dubitable
this does seem to work on linux and the "Bad idea" link leads to an answer to the current question that never actually mentions why this is a bad idea except for one comment that says that conda stores some hard-coded paths (which does answer it, but is not very specific - which paths precisely? maybe it's something that can be ignored?).Inflow

© 2022 - 2024 — McMap. All rights reserved.