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/*