'conda init' 而不关闭当前 shell

jav*_*dba 5 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
Run Code Online (Sandbox Code Playgroud)

这给了我们

 
/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'.
Run Code Online (Sandbox Code Playgroud)

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

mer*_*erv 7

一般来说,以编程方式工作时不需要“激活”环境。这就是conda run为了...

conda run -n py38 python my_script.py
Run Code Online (Sandbox Code Playgroud)

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

eval "$(${CONDA_DIR}/bin/conda shell.bash hook 2> /dev/null)"
Run Code Online (Sandbox Code Playgroud)