首页 > 解决方案 > Conda 4.7 如何在 bash 脚本中激活 env

问题描述

我可以在我的 shell 中激活我的环境

> conda activate env

当我在 bash 脚本中时,我可以检查我的 conda 的版本,但我无法激活我的环境

#!/bin/bash

conda --version
> conda 4.7.12

conda activate env
> 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'.

这是我.bashrc关于 conda 初始化的内容

  8 # User specific aliases and functions
  9 
 10 # >>> conda initialize >>>
 11 # !! Contents within this block are managed by 'conda init' !!
 12 __conda_setup="$('/work/***/miniconda3/bin/conda' 'shell.bash' 'hook' 2> /dev/null)"
 13 if [ $? -eq 0 ]; then
 14     eval "$__conda_setup"
 15 else
 16     if [ -f "/work/***/miniconda3/etc/profile.d/conda.sh" ]; then
 17         . "/work/***/miniconda3/etc/profile.d/conda.sh"
 18     else
 19         export PATH="/work/***/miniconda3/bin:$PATH"
 20     fi
 21 fi
 22 unset __conda_setup
 23 # <<< conda initialize <<<

我从这里找到:Conda 命令在命令提示符下工作,但不在 bash 脚本中,我可以在激活我的 env 之前将其添加到我的 bash 脚本中:

source /work/***/miniconda3/etc/profile.d/conda.sh

它可以工作(我的环境已激活),但我不想在所有 bash 脚本的开头添加这一行,因为我的.bashrc. 我还从这里找到:Python - 通过 shell 脚本激活 conda env.bashrc ,当我运行我的脚本时,我可以使用它

bash -i shell_script.sh

它也在工作(我的环境已激活)。

如何.bashrc在每次运行脚本时自动获取源代码而不在每个脚本中添加一行且不添加-i?我可以用我.bashrc_profile的例子来做这个吗?

标签: bashshellanacondaconda

解决方案


推荐阅读