16

I want to run a script on cluster (SBATCH file).

  1. How can activate my virtual environment (path/to/env_name/bin/activate).

  2. Does I need only to add the following code to my_script.sh file?

    module load python/2.7.14 source "/pathto/Python_directory/ENV2.7_new/bin/activate"

bib
  • 944
  • 3
  • 15
  • 32

2 Answers2

17

You mean to activate a specific Python environment as part of your submission to Slurm? This is what I add to my job script and it works well. Note that I use Anaconda, which by default adds the required paths to my .bashrc script after installation. Hope this helps.

....
# define and create a unique scratch directory
SCRATCH_DIRECTORY=/global/work/${USER}/kelp/${SLURM_JOBID}
mkdir -p ${SCRATCH_DIRECTORY}
cd ${SCRATCH_DIRECTORY}

# Activate Anaconda work environment for OpenDrift
source /home/${USER}/.bashrc
source activate MyEnvironment 

# we execute the job and time it
time mpirun python slurmscript.py
Trond Kristiansen
  • 2,379
  • 23
  • 48
2

Yes. Just make sure to insert those lines after the SBATCH ... lines and before any use of python.

damienfrancois
  • 52,978
  • 9
  • 96
  • 110