Conda 4.7如何在bash脚本中激活环境

时间:2020-01-09 13:38:03

标签: bash shell anaconda conda

我可以在外壳中激活我的环境

> 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 command working in command prompt but not in bash script,可以在激活环境之前添加我的bash脚本:

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

它可以工作(我的环境已激活),但是我不想在所有bash脚本的开头添加此行,因为我已经在.bashrc中添加了它。我还从这里发现:Python - Activate conda env through shell script,当我运行脚本时,可以使用.bashrc

bash -i shell_script.sh

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

如何在每次运行脚本时自动为我的.bashrc提供源,而不在每个脚本中添加一行并且不添加-i?我可以用我的.bashrc_profile来做到这一点吗?

0 个答案:

没有答案