Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

calling conda source activate from bash script

I'm trying to activate my conda env via a bash script. Even though the script runs fine and my PATH appears to be changed within the script, it's getting reset somehow after the script terminates. I can call source activate test from the cmd line and it works fine. An example along with output below.

script:

PycharmProjects/test » cat ./example.sh echo "before calling source: $PATH" source activate test echo "after calling source: $PATH" 

output:

./example.sh before calling source: /Use rs/me/miniconda3/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/TeX/texbin  discarding /Users/me/miniconda3/bin from PATH prepending /Users/me/miniconda3/envs/test/bin to PATH  after calling source: /Users/me/miniconda3/envs/test/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/TeX/texbin` 

but if I echo $PATH after the script finishes, you can see that the $PATH has not changed (i.e. no /Users/me/miniconda3/envs/test/bin):

PycharmProjects/test » echo $PATH /Users/me/miniconda3/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/TeX/texbin

like image 775
matt_k Avatar asked Dec 30 '15 17:12

matt_k


People also ask

How do I run a conda in bash?

for anaconda 4 : bashrc and then copy the path into the file and save it after that you activate the changes using source . bashrc. When I type export PATH=~/anaconda3/bin:$PATH into the terminal and then run conda --version it works fine.

How can we activate conda environment with source?

To activate your Conda environment, type source activate <yourenvironmentname> . Note that conda activate will not work on Discovery with this version. To install a specific package, type conda install -n <yourenvironmentname> [package] . To deactivate the current, active Conda environment, type conda deactivate .

How do you put a conda in the shell?

putting source ~/. bashrc in line 2 (running this should put conda in PATH) putting export PATH="/home/ubuntu/anaconda3/bin:$PATH" in place of line 4.


1 Answers

On more recent versions of conda (4.6+), I have noticed that the following works:

eval "$(conda shell.bash hook)" conda activate <env-name> 
like image 164
Anthony Scopatz Avatar answered Sep 22 '22 22:09

Anthony Scopatz