19

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"

5
  • 2
    Just run the python executable in the bin folder of the virtual environment. Commented Nov 29, 2018 at 18:50
  • can you be more precise please? Commented Nov 29, 2018 at 18:55
  • 1
    I think @KlausD. is suggesting you run something like /path/to/miniconda3/envs/allennlp/bin/python -m allennlp.run --version. Commented Aug 27, 2019 at 15:38
  • did you try using #SBATCH --export=ALL? Commented Nov 11, 2020 at 17:17
  • 1
    Would you mind posting a full example of the solution, please? Commented Feb 9, 2023 at 2:53

2 Answers 2

24

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
Sign up to request clarification or add additional context in comments.

2 Comments

what if my submission script is a python script?
You should be able to just execute your python script where I call mpirun. At that point the Anaconda environment is setup
3

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

3 Comments

what if my submission script is a python script? I can't just do that...
how do i create this environment on ALL nodes of my slurm cluster?
Which lines? Can you post a full example?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.