Python¶
Python is the most widely used programming language for scientific computing. On our clusters, Python environments are managed through Miniforge3, a lightweight conda installer that uses the open conda-forge channel by default.
Getting started¶
Load the Miniforge3 module:
module load Miniforge3
This activates a minimal base environment called miniforge3-base and gives you access to python, conda, pip, and mamba (a faster drop-in replacement for conda). Before creating or installing anything, deactivate this base environment:
conda deactivate
Check the Python version:
python --version
Anaconda3 is no longer supported
We have fully transitioned from Anaconda3 to Miniforge3 due to Anaconda's licensing restrictions. Anaconda3 is no longer installed, maintained, or accessible on our systems.
If you previously loaded module load Python to access Anaconda3, switch to module load Miniforge3. Your existing conda environments will continue to work, but we recommend recreating them with conda-forge packages for long-term compatibility.
Why use conda environments?¶
The base environment provides a minimal Python installation. For your research work, you should create dedicated environments where you can install exactly the packages you need, at the versions you need, without affecting other users or projects.
Environments also make your work reproducible: you can export an environment to a YAML file and recreate it anywhere.
Creating environments¶
Basic environment¶
module load Miniforge3
mamba create --prefix /scratch/$USER/conda-envs/myenv python=3.12
You can include packages at creation time:
mamba create --prefix /scratch/$USER/conda-envs/myenv python=3.12 numpy scipy matplotlib
Activate it by providing the path:
conda activate /scratch/$USER/conda-envs/myenv
Why --prefix instead of --name?
The Miniforge3 module is a system-wide installation where you do not have write permissions. Using conda create --name myenv would try to create the environment inside the Miniforge3 installation directory, which will fail. Always use --prefix to specify a writable location.
If you prefer the convenience of --name, configure envs_dirs in your ~/.condarc to point to a writable directory (see Configuring conda below). After that, conda create --name myenv will create environments there instead.
Make this the default
You can configure conda to always look for environments in /scratch by adding this to your ~/.condarc (see Configuring conda):
envs_dirs:
- /scratch/<username>/conda-envs
After this, conda create --name myenv and conda activate myenv will work without specifying the full path.
Where to create environments
- Atlas EDR and Atlas FDR: your home directory (
/dipc) is not accessible from compute nodes. Environments installed there will not work in SLURM jobs. Always use/scratch. - Hyperion: you can also create environments in
/data(from login nodes), which provides more permanent storage. However,/datais read-only on compute nodes, so environments there must be fully built before submitting jobs.
Create an environment from a YAML file¶
You can define an environment in a YAML file and create it in one step:
mamba env create -f environment.yml
Example environment.yml for PyTorch with GPU support
name: pytorch-gpu
channels:
- pytorch
- nvidia
- conda-forge
dependencies:
- python=3.12
- pytorch=2.5
- torchvision
- torchaudio
- pytorch-cuda=12.4
- numpy
- scipy
- matplotlib
- pandas
- jupyterlab
To install it in /scratch:
mamba env create -f environment.yml --prefix /scratch/$USER/conda-envs/pytorch-gpu
Activating and deactivating environments¶
Activate an environment:
conda activate myenv
Deactivate it and return to base:
conda deactivate
Warning
Activating a conda environment modifies your shell environment in ways that cannot be undone with module unload. To get a clean state, either run conda deactivate or start a new shell session.
Installing packages¶
With conda/mamba¶
Search for a package:
mamba search numpy
Install it in the active environment:
mamba install numpy
Install a specific version:
mamba install numpy=1.26
With pip¶
Some packages are not available through conda-forge. In those cases, use pip inside an active conda environment:
conda activate myenv
pip install package-name
Best practice: conda first, pip second
Always install as many packages as possible with conda/mamba first, then use pip only for what is not available on conda-forge. Mixing the two can cause dependency conflicts. If you need to use both, install all conda packages first, then pip packages last.
Bioinformatics packages (Bioconda)¶
If you work in bioinformatics, genomics, or related fields, the Bioconda channel provides thousands of pre-built packages (samtools, BWA, STAR, DESeq2, snakemake, etc.).
To create an environment with Bioconda packages, specify both the conda-forge and bioconda channels:
mamba create --prefix /scratch/$USER/conda-envs/bioenv \
-c conda-forge -c bioconda \
python=3.12 samtools bwa snakemake
Or in an environment.yml:
name: bioenv
channels:
- conda-forge
- bioconda
dependencies:
- python=3.12
- samtools
- bwa
- snakemake
Note
Bioconda requires conda-forge as a dependency channel. Always list both channels, with conda-forge first.
Exporting and reproducing environments¶
Export your current environment to a YAML file:
conda env export > environment.yml
For a more portable export (without build strings tied to your platform):
conda env export --no-builds > environment.yml
Recreate it on another system or after a scratch cleanup:
mamba env create -f environment.yml
Clone an existing environment:
conda create --prefix /scratch/$USER/conda-envs/myclone --clone /scratch/$USER/conda-envs/myenv
Using conda environments in SLURM jobs¶
When running Python in a SLURM job, you need to load the module and activate the environment in your job script:
#!/bin/bash
#SBATCH --job-name=python-job
#SBATCH --qos=regular
#SBATCH --ntasks=1
#SBATCH --mem=4gb
#SBATCH --output=%x-%j.out
#SBATCH --error=%x-%j.err
module load Miniforge3
conda activate /scratch/$USER/conda-envs/myenv
python my_script.py
Warning
On Atlas EDR and Atlas FDR, make sure your environment is in /scratch (see above).
Two approaches to Python on our clusters¶
There are two ways to use Python on our systems, each with different trade-offs. Understanding the difference will help you choose the right one for your workflow.
Approach 1: Miniforge3 (conda/mamba)¶
This is what most of this page covers. You load Miniforge3, create conda environments, and install packages from conda-forge or pip. This gives you full control over your Python version and packages.
When to use it:
- You need specific package versions or combinations.
- You are working on a project with a
requirements.txtorenvironment.yml. - You need packages that are not available as pre-installed modules (most deep learning frameworks, niche libraries, etc.).
- Portability matters - you want to reproduce the same environment on your laptop or another cluster.
Trade-off: conda packages are pre-compiled generic binaries, not optimized for our specific hardware. For most workloads this makes no noticeable difference, but for code that is heavily dependent on linear algebra (numpy, scipy), the pre-installed system modules can be faster because they are compiled with optimized libraries (MKL, OpenBLAS) tuned for our processors.
Approach 2: Pre-installed system modules (Python + bundles)¶
Our software stack includes optimized Python installations compiled specifically for the cluster hardware. These come with two companion bundles that provide commonly used packages:
SciPy-bundle: scientific core packages (numpy, scipy, pandas, scikit-learn, etc.), compiled against optimized BLAS/LAPACK libraries.Python-bundle-PyPI: a broad collection of general-purpose packages from PyPI (requests, Jinja2, click, pytest, cryptography, and many more).
To use them:
module load SciPy-bundle/2024.05-gfbf-2024a
This automatically loads the corresponding Python version and all dependencies. You can inspect what is included with:
module show SciPy-bundle/2024.05-gfbf-2024a
When to use it:
- Your workflow depends on other system modules (e.g. a simulation code built with a specific toolchain) and you need Python packages that are compatible with them.
- You want optimized numerical performance out of the box (MKL or OpenBLAS linked numpy/scipy).
- You need MPI-aware Python packages like
mpi4pythat must be compiled against the cluster's MPI installation (see MPI for Python).
Trade-off: you are tied to the toolchain and versions we provide. You cannot easily install additional packages into these module-based environments. If you need extra packages on top of a system Python module, you can create a lightweight venv that inherits the module's packages:
module load SciPy-bundle/2024.05-gfbf-2024a
python -m venv --system-site-packages /scratch/$USER/my-venv
source /scratch/$USER/my-venv/bin/activate
pip install some-extra-package
The --system-site-packages flag gives the venv access to all packages from the loaded modules, so you only need to install what is missing.
Which approach should I use?
For most users, Miniforge3 is the easiest and most flexible option. Use pre-installed system modules when you need tight integration with other compiled software on the cluster, or when numerical performance of core scientific libraries is critical to your workflow. The two approaches can coexist - just be careful not to mix them in the same session.
Configuring conda¶
You can customize conda's behavior through the ~/.condarc file:
Example ~/.condarc
# Where conda looks for environments
envs_dirs:
- /scratch/<username>/conda-envs
# Where conda caches downloaded packages
pkgs_dirs:
- /scratch/<username>/conda-pkgs
# Show environment name in the prompt
changeps1: true
env_prompt: '({name}) '
# Default channels
channels:
- conda-forge
Move the package cache to /scratch
Conda caches downloaded packages in your home directory by default, which can fill up your quota quickly. Adding pkgs_dirs pointing to /scratch prevents this.
See also¶
-
MPI for Python (mpi4py)
Using mpi4py with system modules or conda.
-
Environment modules
Managing software and toolchains.
-
Containers (Apptainer)
An alternative for complex or non-portable environments.