ChatGPT解决这个技术问题 Extra ChatGPT

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?

See also this post on how to clone a conda environment
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 --all

b
blaylockbk

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:

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

There is an open issue requesting this feature.


booooo why can't one rename a conda env?
so as to leave some work for those who fork conda and thereby making them understand much more en route :P :D
Another way is to clone the environment conda create --name new_name --clone old_name then you can remove the old one.
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.
@dwanderson +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.
k
kkuma7

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


mvenv () { conda create --name $2 --copy --clone $1 ; conda remove --name $1 --all ;}
(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.)
x
xnx

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


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?
T
Thamme Gowda

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, of course, breaks the hardcoded hashbangs(#!). Hence, we need to go one more level deeper to achieve what we want.

conda env list
# conda environments:
#
base                  *  /home/tgowda/miniconda3
junkdetect               /home/tgowda/miniconda3/envs/junkdetect
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
junkdetect               /home/tgowda/miniconda3/envs/junkdetect
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/*

If trivial it would be great for you to contribute this to the code base! Many would appreciate it (myself included)
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.
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.
L
Long

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

Activate & Export your current environment conda env export > environment.yml 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) 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.


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...?
** 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/env
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.
Make sure that you change both the first line (name: <new name> ) and the final line (prefix: /dir1/dir2/../dirn/<new name>).
C
Chidozie Nnachor

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


Worked. Immediately showed in conda env list. Thanks.
S
Sam Tseng

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

Change the folder name of the old env name into a new env name. 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


B
Bipin Maharjan

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

https://i.stack.imgur.com/pIcxz.png

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.


That is not true for Linux
Bad idea: stackoverflow.com/a/52717925/1506477 explains why
recipe for disaster, don't do that!
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?).