Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to activate a specific Python environment as part of my submission to Slurm?

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"

like image 355
bib Avatar asked Nov 29 '18 18:11

bib


People also ask

How do I enable scripts in Python?

To run Python scripts with the python command, you need to open a command-line and type in the word python , or python3 if you have both versions, followed by the path to your script, just like this: $ python3 hello.py Hello World!

How do you use a Conda in slurm?

To use your Conda environment interactively on a compute node, follow these two steps: Reserve job resources on a node using Slurm's salloc command. Once resources are allocated, activate your Conda environment and run the application.

Why use virtual environment Python?

virtualenv is used to manage Python packages for different projects. Using virtualenv allows you to avoid installing Python packages globally which could break system tools or other projects.


1 Answers

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
like image 68
Trond Kristiansen Avatar answered Sep 26 '22 11:09

Trond Kristiansen