Home > Software engineering >  Using conda activate or specifying python path in bash script?
Using conda activate or specifying python path in bash script?

Time:05-18

I'm running some python scripts on some Linux clusters using SGE or SLURM. I already have my conda environment set up properly using the login node. I have been writing something like

source ~/.bashrc
module purge #Kill all active modules
conda init bash
conda deactivate
conda deactivate
conda activate my_env

python my_script.py

to activate the environment properly. (I have done a lot of work to figure this out) However, I just found some example codes like

/anaconda3/envs/my_env/bin/python my_script.py

seems to do the same thing without the need for tedious deactivation and activation. Are they actually doing the same thing? If so, which would be the better practice?

CodePudding user response:

Programmatic execution with an environment is usually better done through the conda run subcommand. E.g.,

my_slurm_script.sh

#!/bin/bash -l
conda run -n my_env python my_script.py

Read the conda run --help for details.

CodePudding user response:

When you activate an env, it just changes the python executable to /anaconda3/envs/my_env/bin/python instead of the system's python executable /usr/bin/python in layman terms.

A bertter approach will be to use conda's built in method conda run -n env_name script.py.

  • Related