Alina
Alina

Reputation: 21

Activate Anaconda from a bash script?

I am running an R script for the first time on my university's cluster. I am using Anaconda to manage my R packages. I can successfully run the script from the command line but I get a "there is no package called _____" error when I use a bash script to call the same code.

I did a lot of searching around and found this post: Conda command working in command prompt but not in bash script

So I changed my ~/.bashrc from:

export PATH="/home/agarbuzov/anaconda2/bin:$PATH"

to: . /home/agarbuzov/anaconda2/etc/profile.d/conda.sh

And that didn't help. I don't have a lot of experience using conda -- I've just run a few jobs on the cluster. Any guidance would be greatly appreciated because I have no ideas here.

This is what my test script looks like:

#!/bin/csh
#PBS -q hotel
#PBS -l nodes=1:ppn=1
#PBS -l walltime=1:00:00
#PBS -N tom_bootstraps
#PBS -o tomboot_output.txt
#PBS -e tomboot_err.txt
#PBS -V
#PBS -M ***
#PBS -m abe

source /home/agarbuzov/anaconda2/etc/profile.d/conda.sh

conda activate r_env

Rscript ~/ascripts/1_rWGCNA_bootstrap_test.R

All the packages I need are listed when I call $conda list.

$conda info

     active environment : r_env
    active env location : /home/agarbuzov/anaconda2/envs/r_env
            shell level : 1
       user config file : /home/agarbuzov/.condarc
 populated config files : /home/agarbuzov/.condarc
          conda version : 4.6.8
    conda-build version : 1.21.3
         python version : 2.7.15.final.0
       base environment : /home/agarbuzov/anaconda2  (writable)
           channel URLs : https://conda.anaconda.org/bioconda/linux-64
                          https://conda.anaconda.org/bioconda/noarch
                          https://conda.anaconda.org/conda-forge/linux-64
                          https://conda.anaconda.org/conda-forge/noarch
                          https://repo.anaconda.com/pkgs/main/linux-64
                          https://repo.anaconda.com/pkgs/main/noarch
                          https://repo.anaconda.com/pkgs/free/linux-64
                          https://repo.anaconda.com/pkgs/free/noarch
                          https://repo.anaconda.com/pkgs/r/linux-64
                          https://repo.anaconda.com/pkgs/r/noarch
          package cache : /home/agarbuzov/anaconda2/pkgs
                          /home/agarbuzov/.conda/pkgs
       envs directories : /home/agarbuzov/anaconda2/envs
                          /home/agarbuzov/.conda/envs
               platform : linux-64
             user-agent : conda/4.6.8 requests/2.21.0 CPython/2.7.15 Linux/2.6.32-696.18.7.el6.x86_64 centos/6.6 glibc/2.12
                UID:GID : 520822:10494
             netrc file : None
           offline mode : False

Upvotes: 1

Views: 3239

Answers (1)

abalter
abalter

Reputation: 10393

I'm trying to remember where I found this, but there is a snippet of code, I don't understand what it does, but this fixes the problem.

eval "$(conda shell.bash hook)"

Alternatively, with some cluster management software (HTCondor, Slurm, ...) you can specify to run your jobs using your home environment.

Alternatively, you could also try source'ing you .bashrc in your submission script.

At any rate, the first method does seem to work.

Upvotes: 1

Related Questions