"conda init"而不关闭当前外壳

  • 本文关键字:外壳 conda init python conda
  • 更新时间 :
  • 英文 :


有许多用例,我试图使用conda。最令人头痛的是conda init不希望在同一个bash shell中的脚本流中公平地发挥作用。。我经常看到

CommandNotFoundError:您的shell没有正确配置为使用'conda activate'.

即使conda初始化脚本的内容以及conda init已经执行,也会发生这种情况。结果是不一致的:有时我看到init逻辑工作了,而另一些则没有。我还不能确定正在发生什么魔法,以及它需要什么才能正常工作。

conda_init() {
# __conda_setup="$('$CONDA_DIR/bin/conda' 'shell.bash' 'hook' 2> /dev/null)";
source $CONDA_DIR/etc/profile.d/conda.sh
export PATH="$CONDA_DIR/condabin:$CONDA_DIR/bin:$PATH";
export CONDARC=$CONDA_DIR ;
conda init bash
}
conda_init
conda activate py38

得到


/Users/steve/opt/miniconda3/bin/conda
/Users/steve/opt/miniconda3/bin::/Users/steve/opt/miniconda3/condabin:<other stuff..>
CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'.
To initialize your shell, run
$ conda init <SHELL_NAME>
Currently supported shells are:
- bash
- fish
- tcsh
- xonsh
- zsh
- powershell
See 'conda init --help' for more information and options.
IMPORTANT: You may need to close and restart your shell after running 'conda init'.

如何使conda_init()能够可靠地在同一个shell中运行conda init?

一般来说,不需要"激活";以编程方式工作时的环境。这就是conda run的作用

conda run -n py38 python my_script.py

否则,如果定义了CONDA_DIR,那么以下命令将在活动的bash会话中运行初始化shell命令:

eval "$(${CONDA_DIR}/bin/conda shell.bash hook 2> /dev/null)"

最新更新