简体   繁体   English

如何重命名 conda 环境?

[英]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:请注意,这种方法有几个缺点:

  1. It redownloads packages (you can use --offline flag to disable it)它重新下载包(你可以使用--offline标志来禁用它)
  2. Time consumed on copying environment's files复制环境文件所消耗的时间
  3. Temporary double disk usage临时双盘使用

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的回答提到了一些缺点:在我看来,无痛且无风险(解决方法)的方法是遵循以下步骤:

  1. Activate & Export your current environment conda env export > environment.yml激活并导出您当前的环境conda env export > environment.yml
  2. Deactivate current conda environment.停用当前的 conda 环境。 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)修改environment.yml文件并根据需要更改环境名称(通常在yaml文件的第一行)
  3. Create a new conda environment by executing this 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_namenew_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:无法重命名活动环境

  • If one tries to rename using an existing environment name, one will get a 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:

  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.替换新 env 文件夹中 bin 文件夹下所有常规文件的 shebang 行中的所有旧 env 名称。

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:这是证明:

Conda 环境重命名

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 列表及其位置。

Just open the folder"\\Anaconda\\envs" and rename it. 只需打开文件夹“ \\ Anaconda \\ envs”并重命名即可。

Before Rename 重命名之前 重命名之前

After Rename 重命名后 重命名后

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM