[英]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?我有一个名为old_name
的 conda 环境,如何在不破坏引用的情况下将其名称更改为new_name
?
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在 Windows 上deactivate
或source deactivate
on macOS/Linux.在 macOS/Linux 上source deactivate
。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:请注意,这种方法有几个缺点:
--offline
flag to disable it)它重新下载包(你可以使用--offline
标志来禁用它)There is an open issue requesting this feature.有一个请求此功能的未解决问题。
conda create --name new_name --copy --clone old_name
is better conda create --name new_name --copy --clone old_name
更好
I use conda create --name new_name --clone old_name
which is without --copy
but encountered pip breaks...我使用conda create --name new_name --clone old_name
没有--copy
但遇到点中断...
the following url may help Installing tensorflow in cloned conda environment breaks conda environment it was cloned from以下 url 可能有助于在克隆的 conda 环境中安装 tensorflow 会破坏从中克隆的 conda 环境
Based upon dwanderson 's helpful comment, I was able to do this in a Bash one-liner:根据dwanderson的有用评论,我能够在 Bash 单行中做到这一点:
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".我命名错误的环境是“env1”,而我希望从中克隆的新环境是“envpython2”。
conda should have given us a simple tool like cond env rename <old> <new>
but it hasn't. conda 应该给我们一个简单的工具,比如cond env rename <old> <new>
但它没有。 Simply renaming the directory, as in this previous answer , of course, breaks the hardcoded hashbangs(#!).简单地重命名目录,就像在前面的答案中一样,当然会破坏硬编码的 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)在这里我试图重命名rtg
--> unsup
(请忍受这些名字,这是我真正的用例)
$ 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.待处理的任务是, $NEW/bin
目录有一堆可执行脚本,其中包含指向 $OLD env 路径的 hashbangs ( #!
)。
See jupyter
, for example:参见jupyter
,例如:
$ 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 轻松修复它
$ 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
.我认为编写一个可移植的脚本来完成所有这些并将其绑定到conda env rename old new
应该是微不足道的。
I tested this on ubuntu.我在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/*
As the answer from @pkowalczyk mentioned some drawbacks: In my humble opinion, the painless and risk-free (workaround) way is following these steps instead:正如@pkowalczyk的回答提到了一些缺点:在我看来,无痛且无风险(解决方法)的方法是遵循以下步骤:
conda env export > environment.yml
激活并导出您当前的环境conda env export > environment.yml
environment.yml
file and change the name of the environment as you desire (usually it is on the first line of the yaml
file)修改environment.yml
文件并根据需要更改环境名称(通常在yaml
文件的第一行)conda env create -f environment.yml
通过执行这个conda env create -f environment.yml
创建一个新的 conda 环境This process takes a couple of minutes, and now you can safely delete the old environment.此过程需要几分钟,现在您可以安全地删除旧环境。
PS nearly 5 years and conda still does not have its "rename" functionality. PS 将近 5 年了,conda 仍然没有它的“重命名”功能。
I'm using Conda on Windows and this answer did not work for me.我在 Windows 上使用 Conda,这个答案对我不起作用。 But I can suggest another solution:但我可以建议另一种解决方案:
rename enviroment folder ( old_name
to new_name
)重命名环境文件夹( old_name
为new_name
)
open shell and activate env with custom folder:打开 shell 并使用自定义文件夹激活 env:
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:例如,更新 numpy:
conda update numpy
after applying any action to package, the environment will show in env list.对包应用任何操作后,环境将显示在 env 列表中。 To check this, type:要检查这一点,请键入:
conda env list
For that one can access the base/root environment and use conda rename
.为此,可以访问基本/根环境并使用conda rename
。
Assuming one's environment is stack
and one wants the name lab
, one can do the following假设一个人的环境是stack
并且想要名称lab
,可以执行以下操作
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
如果在环境stack
的提示中,将无法执行上述操作,并且会CondaEnvException
CondaEnvException: Cannot rename the active environment CondaEnvException:无法重命名活动环境
CondaEnvException
.如果尝试使用现有环境名称重命名,则会收到CondaEnvException
。 Using the case above, one will get使用上面的例子,你会得到CondaEnvException: The environment 'lab' already exists. CondaEnvException:环境“实验室”已经存在。 Override with --force.用 --force 覆盖。
According to the answer of Thamme Gowda, the following steps work for me on my MacBook Pro:根据 Thamme Gowda 的回答,以下步骤适用于我的 MacBook Pro:
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:检查shebang线是否正确更换:
$ head -1 $(which jupyter)
#!/Users/my_username/anaconda3/envs/new_name/bin/python
$ head -1 $(which jupyter)
#!/Users/my_username/anaconda3/envs/new_name/bin/python
Locate your environment folder in anaconda3/envs. 在anaconda3 / envs中找到您的环境文件夹。 Rename the environment folder to your desired name. 将环境文件夹重命名为所需的名称。
You can rename your Conda env by just renaming the env folder.您只需重命名 env 文件夹即可重命名您的 Conda env。 Here is the proof:这是证明:
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.您可以在C:\ProgramData\Anaconda3\envs
中找到您的 Conda env 文件夹,或者您可以输入conda env list
以查看 conda envs 列表及其位置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.