bash 默认情况下,如何防止 Conda 激活基础环境?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/54429210/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I prevent Conda from activating the base environment by default?
提问by DryLabRebel
I recently installed anaconda2 on my Mac. By default Conda is configured to activate the base environment when I open a fresh terminal session.
我最近在 Mac 上安装了 anaconda2。默认情况下,Conda 配置为在我打开新的终端会话时激活基本环境。
I want access to the Conda commands (i.e. I want the path to Conda added to my $PATH which Conda does when initialised so that's fine).
我想访问 Conda 命令(即,我希望将 Conda 的路径添加到我的 $PATH 中,Conda 在初始化时会这样做,这样很好)。
But I don't ordinarily program in python, and I don't want Conda to activate an environment by default.
但是我一般不会用python编程,也不希望Conda默认激活一个环境。
When first executing conda init
from the prompt, Conda adds the following to my .bash_profile
:
首次conda init
从提示执行时,Conda 将以下内容添加到 my .bash_profile
:
# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$('/Users/geoff/anaconda2/bin/conda' 'shell.bash' 'hook' 2> /dev/null)"
if [ $? -eq 0 ]; then
eval "$__conda_setup"
else
if [ -f "/Users/geoff/anaconda2/etc/profile.d/conda.sh" ]; then
. "/Users/geoff/anaconda2/etc/profile.d/conda.sh"
else
export PATH="/Users/geoff/anaconda2/bin:$PATH"
fi
# fi
unset __conda_setup
# <<< conda initialize <<<
If I comment out the whole block, then I can't activate any Conda environments.
如果我注释掉整个块,那么我将无法激活任何 Conda 环境。
I tried to comment out the whole block except for
我试图注释掉整个块,除了
export PATH="/Users/geoff/anaconda2/bin:$PATH"
But then when I started a new session and tried to activate an environment, I got this error message:
但是当我开始一个新的会话并尝试激活一个环境时,我收到了这个错误消息:
CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'.
This question(and others like it) are helpful, but doesn't ultimately answer my question and is more suited for linux users.
这个问题(以及其他类似问题)很有帮助,但最终并没有回答我的问题,更适合 linux 用户。
For clarification, I'm not asking to remove the (base)
from my $PS1
I'm asking for Conda not to activate base when I open a terminal session.
为了澄清起见,我不是要求(base)
从我$PS1
要求 Conda 在打开终端会话时不要激活 base 中删除。
回答by jieong
I have conda 4.6 with a similar block of code that was added by conda. In my case, there's a conda configuration setting to disable the automatic base activation:
我有 conda 4.6,其中包含由 conda 添加的类似代码块。就我而言,有一个 conda 配置设置来禁用自动基本激活:
conda config --set auto_activate_base false
The first time you run it, it'll create a ./condarc
in your home directory with that setting to override the default.
第一次运行它时,它会./condarc
在您的主目录中创建一个使用该设置覆盖默认值的设置。
This wouldn't de-clutter your .bash_profile
but it's a cleaner solution without manual editing that section that conda manages.
这不会使您的内容.bash_profile
变得混乱,但它是一种更简洁的解决方案,无需手动编辑 conda 管理的部分。
回答by darthbith
The answer depends a little bit on the version of conda
that you have installed. For versions of conda >= 4.4, it should be enough to deactivate
the conda environment after the initialization, so add
答案在一定程度上取决于conda
您安装的版本。conda>=4.4的版本,deactivate
初始化后的conda环境应该就够了,所以添加
conda deactivate
right underneath
正下方
# <<< conda initialize <<<
回答by DryLabRebel
So in the end I found that if I commented out the Conda initialisation block like so:
所以最后我发现,如果我像这样注释掉 Conda 初始化块:
# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
# __conda_setup="$('/Users/geoff/anaconda2/bin/conda' 'shell.bash' 'hook' 2> /dev/null)"
# if [ $? -eq 0 ]; then
# eval "$__conda_setup"
# else
if [ -f "/Users/geoff/anaconda2/etc/profile.d/conda.sh" ]; then
. "/Users/geoff/anaconda2/etc/profile.d/conda.sh"
else
export PATH="/Users/geoff/anaconda2/bin:$PATH"
fi
# fi
# unset __conda_setup
# <<< conda initialize <<<
It works exactly how I want. That is, Conda is available to activate an environment if I want, but doesn't activate by default.
它完全按照我想要的方式工作。也就是说,如果需要,Conda 可用于激活环境,但默认情况下不会激活。
回答by Simba
There're 3 ways to achieve this after conda
4.6. (The last method has the highest priority.)
在conda
4.6之后有 3 种方法可以实现这一点。(最后一种方法具有最高优先级。)
Use sub-command
conda config
to change the setting.conda config --set auto_activate_base false
In fact, the former
conda config
sub-command is changing configuration file.condarc
. We can modify.condarc
directly. Add following content into.condarc
under your home directory,# auto_activate_base (bool) # Automatically activate the base environment during shell # initialization. for `conda init` auto_activate_base: false
Set environment variable
CONDA_AUTO_ACTIVATE_BASE
in the shell's init file. (.bashrc
for bash,.zshrc
for zsh)CONDA_AUTO_ACTIVATE_BASE=false
To convert from the
condarc
file-based configuration parameter name to the environment variable parameter name, make the name all uppercaseand prependCONDA_
. For example, conda'salways_yes
configuration parameter can be specified using aCONDA_ALWAYS_YES
environment variable.The environment settings take precedenceover corresponding settings in
.condarc
file.
使用子命令
conda config
更改设置。conda config --set auto_activate_base false
实际上,前一个
conda config
子命令正在更改配置文件.condarc
. 我们可以.condarc
直接修改。将以下内容添加到.condarc
您的主目录下,# auto_activate_base (bool) # Automatically activate the base environment during shell # initialization. for `conda init` auto_activate_base: false
CONDA_AUTO_ACTIVATE_BASE
在 shell 的 init 文件中设置环境变量。(.bashrc
对于 bash,.zshrc
对于 zsh)CONDA_AUTO_ACTIVATE_BASE=false
要将
condarc
基于文件的配置参数名称转换为环境变量参数名称,请将名称全部设为大写并在前面加上CONDA_
。例如,always_yes
可以使用CONDA_ALWAYS_YES
环境变量指定conda 的配置参数。环境设置优先于
.condarc
文件中的相应设置。
References
参考
- The Conda Configuration Engine for Power Users
- Using the .condarc conda configuration file
conda config --describe
- Conda 4.6 Release
- 高级用户的 Conda 配置引擎
- 使用 .condarc conda 配置文件
conda config --describe
- 康达 4.6 发布
回答by kudibaba
To disable auto activation of conda base environment in terminal:
要在终端中禁用 conda 基础环境的自动激活:
conda config --set auto_activate_base false
To activate conda base environment:
要激活 conda 基础环境:
conda activate
回答by coldfix
If you want to keep your bashrc simple, you can remove all the conda init
generated clutter, and keep only a single line:
如果你想保持你的 bashrc 简单,你可以删除所有conda init
生成的混乱,只保留一行:
. "/Users/geoff/anaconda2/etc/profile.d/conda.sh"
See Recommended change to enable conda in your shell.
This will make the conda command available without activating the base environment.
这将使 conda 命令可用而无需激活基本环境。
If you want to use your bashrc on other systems where conda is not installed in the same path, you can keep the if
/fi
lines as well to avoid error messages, i.e.:
如果要在 conda 未安装在同一路径中的其他系统上使用 bashrc,也可以保留if
/fi
行以避免错误消息,即:
if [ -f "/Users/geoff/anaconda2/etc/profile.d/conda.sh" ]; then
. "/Users/geoff/anaconda2/etc/profile.d/conda.sh"
fi
回答by Manideep Pullalachervu
I faced the same problem. Initially I deleted the .bash_profile but this is not the right way. After installing anaconda it is showing the instructions clearly for this problem. Please check the image for solution provided by Anaconda
我遇到了同样的问题。最初我删除了 .bash_profile 但这不是正确的方法。安装 anaconda 后,它清楚地显示了此问题的说明。请检查图像以获取 Anaconda 提供的解决方案
回答by CcMango
This might be a bug of the recent anaconda. What works for me:
这可能是最近 anaconda 的一个错误。什么对我有用:
step1: vim /anaconda/bin/activate
, it shows:
step1: vim /anaconda/bin/activate
,显示:
#!/bin/sh
_CONDA_ROOT="/anaconda"
# Copyright (C) 2012 Anaconda, Inc
# SPDX-License-Identifier: BSD-3-Clause
\. "$_CONDA_ROOT/etc/profile.d/conda.sh" || return $?
conda activate "$@"
step2: comment out the last line: # conda activate "$@"
step2:注释掉最后一行:# conda activate "$@"