Skip to content

Using Conda Environments in Scripts

Activation of your environment is really only needed for package installation. For using the environment just add the path to its bin directory to $PATH in your job script.

E.g. If you conda environment at /blue/mygroup/$USER/conda/envs/project1/, add the following into your job script before executing any commands

export PATH=/blue/mygroup/$USER/conda/envs/project1/bin:$PATH
Expand to see the details of a sample script with the export command
#!/bin/bash 
#SBATCH --ntasks=1                    # Run on a single CPU
#SBATCH --mem=1gb                     # Job memory request
#SBATCH --time=00:05:00               # Time limit hrs:min:sec
#SBATCH --output=serial_test_%j.log   # Standard output and error log

pwd; hostname; date

# Set path to environment we want and pre-pend to PATH variable
env_path=/blue/mygroup/username/.conda/envs/my_env/bin/
export PATH=$env_path:$PATH

# Run my amazing python script using the my_env environment
python amazing_script.py

Adjust the path as needed for your environment. The path should include the bin folder as shown above.

Get the Python Path for Name-Based EnvironmentsΒΆ

If you are using name-based environments, it is still best practice to add the path, rather than activating the environment. You can find the proper path by activating the environment and using the which python command.

Expand to see the details of getting the Python path from a name-based environment
[username@login8 ~]$ module load conda
[username@login8 ~]$ mamba activate my_env
(my_env) [username@login8 ~]$ which python
/blue/mygroup/username/.conda/envs/my_env/bin/python
(nlp_test) [username@login8 ~]$

In this case, the path we would want to add is /blue/mygroup/username/.conda/envs/my_env/bin/