Managing Python environments in HPC can be challenging due to conflicting dependencies and version requirements. Apptainer solves this by encapsulating the entire environment.
- System Python: Installing packages directly into
/usr/lib/pythonX.Yusingpip(as root during build). Good for small images. - Conda/Mamba: Creating a self-contained Conda environment. Preferred for data science as it handles non-Python dependencies (e.g., CUDA toolkits, C libraries).
- Clean up: Always run
conda clean --allorpip cache purgeto keep image size down. - Environment Variables: Set
PATHcorrectly in%environmentso the container uses your custom Python by default. - Reproducibility: Use
requirements.txtorenvironment.ymlfiles.
In this exercise, we will build a container with a specific version of Python and some common libraries (numpy, pandas) using Miniconda.
See exercises/02_conda.def.
- Installation: We download and install Miniconda in
%post. - Path Management: We add the conda bin directory to
$PATHin%environment.
Once built, you can run scripts easily:
apptainer exec conda_env.sif python script.py