Skip to content

Python

Python is a general-purpose programming language widely used for data analysis, machine learning, scientific computing, and workflow automation. It is often combined with scientific libraries such as NumPy, SciPy, pandas, and packages for numerical or parallel computing.

On HPC Vega, Python is provided as:

  • an environment module;
  • part of a Conda/Anaconda/Miniconda distribution;
  • user-managed virtual environments;
  • a container (Apptainer images with Python pre-installed).

For preparing and editing Python scripts on HPC Vega, the most practical approach is to use the Visual Studio Code (VS Code) editor. You can connect to HPC Vega directly from VS Code via the Remote SSH extension. This allows you to edit files directly on the cluster. This means you can iteratively modify code, review execution results, and submit SLURM jobs from the same workspace, without needing to transfer files between your local computer and the cluster. Instructions for setting up Remote SSH in VS Code can be found here.

For working with Python in Visual Studio Code, it is recommended to install the Python, Pylance, Python Debugger and Python Environments extensions.

Installation and usage

On HPC Vega Python is available through environment modules, so a local installation is not needed. To check the available versions use the command:

module avail python

This lists all Python-related modules available on the system.

To load the Python module, use the command

module load Python/<version>

Replace <version> with the exact version shown by module avail.

To verify that the module was loaded successfully, use the commands:

python --version
which python

This confirms the Python interpreter is loaded and shows its location.

Setting up a Python virtual environment

This example shows workflow for running a Python script using a virtual environment.

1. Loading a Python interpreter

module load Python/<version>

Loads the centrally installed Python interpreter.

2. Create a virtual environment

python -m venv myenv

Creates an isolated Python environment in a folder called myenv, so installed packages don't affect the system Python or other users.

3. Activate the environment

To activate the created virtual environment, use the command:

source myenv/bin/activate

After activating your shell prompt will change to show (myenv).

4. Install required packages

Before installing the packages, check that you are using pip from your local virtual environment:

which pip

Then update pip inside the activated virtual environment:

pip install --upgrade pip

To install Python packages inside your virtual environment, use the pip install command:

pip install <package>

You can also use pip install with the requirements.txt file as:

pip install -r requirements.txt

Installed packages are active only in the virtual environment.

5. Updating and freezing the virtual environment

Updating pip and packages

The pip that the virtual environment inherits from the Python module may be older than the latest version. Therefore, update it after creating the environment:

source myenv/bin/activate
pip install --upgrade pip

Check the list of installed and outdated packages with the following commands:

pip list
pip list --outdated

Update an individual package with:

pip install --upgrade <package>

Freezing packages (pip freeze)

For reproducible computations, save the list of installed packages and their versions to a requirements.txt file:

pip freeze > requirements.txt

You can then recreate the same environment in a new folder (or on another cluster) with the following commands:

module load Python/<version>
python -m venv new-env
source new-env/bin/activate
pip install -r requirements.txt

6. Run the script

To run the script, use the SLURM job scheduler. You can submit your job using a .sh script. Below is an example of such script:

#!/bin/bash
#SBATCH --job-name=python_job        
#SBATCH --output=python_job_%j.out     
#SBATCH --error=python_job_%j.err   
#SBATCH --nodes=1                   
#SBATCH --ntasks=1                   
#SBATCH --cpus-per-task=1
#SBATCH --time=00:30:00

module load Python/3.10.8-GCCcore-12.2.0
source myenv/bin/activate

python my_script.py

deactivate

Submitting the job:

sbatch my_job.sh

You can check the job status using the command:

squeue --me

Output and error logs will appear in the files defined by --output and --error. Any additional files your script writes (plots, CSVs, checkpoints, etc.) will appear in the locations you specified in the source code. If you did not specify a path, they will be saved in the working directory from which the job was submitted.

Conda

Python applications can also be executed using Anaconda/Conda environments. More information is available here.

Containers

Python applications can also be executed inside containers, which provide a portable and reproducible software environment with all required dependencies pre-installed. For more information on using containers on Vega, see the Containers documentation.