Using isolated environments

Learning objectives

  • Practice using the documentation of your HPC cluster

  • Find out which isolated environment tool to use on your HPC cluster

  • Work (create, activate, work, deactivate) with isolated environments in the way recommended by your HPC cluster

  • (optional) work (create, activate, work, deactivate) with isolated environments in the other way (if any) possible on your HPC cluster

  • (optional) export and import a virtual environment

Why isolated environments are important

Isolated/virtual environments solve a couple of problems:

  • You can install specific, also older, package versions into them.

  • You can create one for each project and no problem if the two projects require different versions.

  • You can remove the environment and create a new one, if not needed or with errors.

  • Good for reproducibility!

When?

  • When installed modules are not enough for you.

  • Examples:

    • you continue a project on your computer on the HPC cluster

    • you continue someone else’s project on the HPC cluster

Discussion

  • What experience have you had?

What is an isolated environments

  • Isolated environments let you create separate workspaces for different versions of Python and/or different versions of packages.

  • You can activate and deactivate them one at a time, and work as if the other workspace does not exist.

Principles

  • create environment (choose Python version as well)

  • activate the environment

    • now you work isolated from the rest of the system, e.g. Python packages

  • install packages

    • these are now reached only from the activated project

  • do your research

  • deactivate

The tools

  • Python’s built-in venv module: uses pip

  • virtualenv (in bundle module or can be installed): uses pip

  • conda/forge: uses conda and mamba

Tool

Software module

Own Python

Can use python packages from outside

venv

Python-

Uses the one it was activated from

--system-site-packages

conda

conda/forge

Yes

No

What happens at activation?

  • Python version is defined by the environment.

    • Check with which python, should show at path to the environment.

  • Packages are defined by the environment.

    • Check with pip list

    • conda can only see what you installed for it.

    • venv and virtualenv also see other packages if you allowed for that when creating the environment (--system-site-packages).

  • You can work in a Python shell or IDE (coming session)

  • You can run scripts dependent on packages now installed in your environment.

Warning

About Conda on HPC systems

  • Conda is good in many ways but can interact negatively when

    • using the python modules (module load) at the same time

    • having base environment always active

  • Not recommended at HPC2N

  • At the other clusters, handle with care!

HPC cluster

Conda vs venv

Alvis

venv, conda in container

Bianca

conda/latest, venv via wharf

COSMOS

Anaconda3/2024.02-1

Dardel

miniconda3/25.3.1-1-cpeGNU-24.11

Kebnekaise

venv only

LUMI

venv, conda-containerize

Pelle

venv, Miniforge3/24.11.3-0

Tetralith

Anaconda3/2024.02-1

LUMI

conda-containerize

Tip

  • Try with venv first

  • If very troublesome, try with conda

  • To use self-installed Python packages in a batch script, you also need to load the above mentioned modules and activate the environment. An example of this will follow later in the course.

  • To see which Python packages you have installed in an environment, you can use pip list --local while the environment you have installed the packages in is active. To see all packages, use pip list.

    • Note that --user must be omitted: else the package will be installed in the global user folder.

Virtual environment - venv & virtualenv

With this tool you can download and install with pip from the PyPI repository

Tip for Tetralith

  • load a “bare” python, like Python/3.10.4-bare-hpc1-gcc-2022a-eb

  • in an environment install setuptools and wheel: pip3 install --upgrade pip setuptools wheel

Example NSC

      ml buildtool-easybuild/4.8.0-hpce082752a2 GCC/13.2.0 Python/3.11.5
      which python
      python -V
      cd /proj/courses-fall-2025/users/<username>
      python -m venv env-matplotlib
      source env-matplotlib/bin/activate
      #note that the prompt has ``(env-matplotlib)``
      #install some extra pip related tools
      #pip3 install --upgrade pip setuptools wheel
      pip install matplotlib
      # do some work
      python

    >>> import matplotlib
  • When work is done, deactivate the environment with

   deactivate

Conda

  • Conda is an installer of packages but also bigger toolkits and is useful also for R packages and C/C++ installations.

  • Conda creates isolated environments not clashing with other installations of python and other versions of packages.

  • Conda environment requires that you install all packages needed by yourself.

    • That is, you cannot load the Python module and use the packages therein inside you Conda environment.

One can choose from different repos, or channels

Miniconda and Miniforge are the common software modules at the centres.

Warning

Drawbacks

  • Conda cannot use already installed packages from the Python modules and libraries, and hence installs them anyway

  • Conda is therefore known for creating many small files. Your disk space is not only limited in GB, but also in number of files (typically 300000 in $HOME).

  • Check your disk usage and quota limit

    • Do a conda clean -a once in a while to remove unused and unnecessary files

Tip

  • The conda environments including many small files are by default stored in ~/.conda folder that is in your $HOME directory with limited storage.

  • Use the commands

export CONDA_ENVS_PATH="path/to/your/project/(subdir)"
export CONDA_PKG_DIRS="path/to/your/project/(subdir)"
mamba create --prefix=$CONDA_ENVS_PATH/<conda env name>

Example NSC

module load Miniforge/24.7.1-2-hpc1
export CONDA_PKG_DIRS=/proj/spring-courses-naiss/users/$USER
export CONDA_ENVS_PATH=/proj/spring-courses-naiss/users/$USER
mamba create --prefix=$CONDA_ENVS_PATH/numpy-proj-39 python=3.9.5 -c conda-forge
mamba activate nsc-example
# A prompt "(/path-to/nsc-example/)" should show up
# double-check we are using python from the Conda environment!
which python  # should point to the conda environment!
python -V     # should give python version 3.9.5
conda install numpy

>>> import numpy
  • Note, when pinning with conda, use single = instead of double (as used by pip)

Install from file

  • This is handy when you want to move your Python environment somewhere else.

  • Also, when giving your code to someone else (in research group or to a community).

  • Good for reproducibility

Principle for both venv & conda

  1. Activate the environment and make sure it works

  2. Save a environment file

  3. Distribute the file

  4. Receiver installs the environment from the file

venv

Make a requirements file:

pip freeze --local > requirements.txt

Install packages from a file

   pip install -r requirements.txt

conda/forge

Make environment file:

    conda env export > environment.yml

Create an environment from a file. Do this on another computer or rename.

   conda env create -f environment.yml
   # if renaming is necessary
   conda env create -f environment.yml -p /path/renamed

Discussion

  • Did you note the difference in file length for venv & conda?

  • What does it mean?

Exercises

  • All centers has had different approaches in what is included in the module system and not.

  • Therefore the solution to complete the necessary packages needed for the course lessons, different approaches has to be made.

  • This is left as exercise for you, see Exercise 4 and 5.

Exercise 0: Make a decision between venv or conda.

  • We recommend conda for LUNARC.

  • We recommend venv for HPC2N

  • Otherwise there are some kind of documentation at all sites.

  • venv “should” work everywhere but has not been fully tested

Breakout room according to grouping

Exercise 2: Prepare the course environment

There will be a mix of conda and venv att all clusters except for HPC2N where all is venv

  1. Let’s make a Spyder installation in a conda environment <https://saturncloud.io/blog/how-to-ensure-that-spyder-runs-within-a-conda-environment/#step-2-create-a-conda-environment>_

   module load Miniforge/24.7.1-2-hpc1
   export CONDA_PKG_DIRS=/proj/spring-courses-naiss/users/$USER
   export CONDA_ENVS_PATH=/proj/spring-courses-naiss/users/$USER
   mamba create --prefix=$CONDA_ENVS_PATH/spyder-env python=3.12 spyder
   mamba activate spyder-env
   # A prompt "(/path-to/spyder-env/)" should show up
   # double-check we are using python from the Conda environment!
   which python  # should point to the conda environment!
   python -V     # should give python version 3.12.X
  • Let’s install packages that we need.

   conda install matplotlib pandas seaborn xarray dask numba
  • Check that the above packages are there by conda list.

We will put requirements files in the course project folder that you can build from in latter lessons.

  • These will cover

    • TensorFlow

    • PyTorch

    • numba

  1. Let’s make a Spyder installation in a conda environment <https://saturncloud.io/blog/how-to-ensure-that-spyder-runs-within-a-conda-environment/#step-2-create-a-conda-environment>_

   ml PDC/24.11
   ml miniconda3/25.3.1-1-cpeGNU-24.11
   export CONDA_ENVS_PATH="/cfs/klemming/projects/supr/spring-courses-naiss/$USER/" #only needed once per session
   export CONDA_PKG_DIRS="/cfs/klemming/projects/supr/spring-courses-naiss/$USER/" #only needed once per session
   conda create --prefix $CONDA_ENVS_PATH/spyder-env python=3.11.7 spyder
   source activate spyder-env
   # A prompt "(/path-to/spyder-env/)" should show up
   # double-check we are using python from the Conda environment!
   which python  # should point to the conda environment!
   python -V     # should give python version 3.12.X
  • Let’s install packages that we need.

   conda install matplotlib pandas seaborn xarray dask numba
  • Check that the above packages are there by conda list.

  1. Let’s make a Jupyter installation based on Python 3.11.7

   ml PDC/24.11
   ml miniconda3/25.3.1-1-cpeGNU-24.11
   export CONDA_ENVS_PATH="/cfs/klemming/projects/supr/spring-courses-naiss/$USER/" #only needed once per session
   export CONDA_PKG_DIRS="/cfs/klemming/projects/supr/spring-courses-naiss/$USER/" #only needed once per session
   conda create --prefix $CONDA_ENVS_PATH/jupyter-env python=3.11.7 jupyter
   conda activate jupyter-env
   # A prompt "(/path-to/jupyter-env/)" should show up
   # double-check we are using python from the Conda environment!
   which python  # should point to the conda environment!
   python -V     # should give python version 3.11.7
  • Let’s install packages that we need.

   conda install matplotlib pandas seaborn xarray dask numba
  • Check that the above packages are there by conda list.

We will put requirements files in the course project folder that you can build from in latter lessons

  • These will cover

    • TensorFlow

    • PyTorch

    • numba

  • Everything will work by just loading modules.

  • Go down to the other exercises!

Let’s make a Spyder installation in a conda environment <https://saturncloud.io/blog/how-to-ensure-that-spyder-runs-within-a-conda-environment/#step-2-create-a-conda-environment>_

   ml conda
   export CONDA_PKG_DIRS=/proj/<proj-dir>/$USER
   export CONDA_ENVS_PATH=/proj/<proj-dir>/$USER
   conda create --prefix $CONDA_ENVS_PATH/spyder-env python=3.12 spyder -c conda-forge
   source activate spyder-env
   # A prompt "(/path-to/spyder-env/)" should show up
   # double-check we are using python from the Conda environment!
   which python  # should point to the conda environment!
   python -V     # should give python version 3.12.X
   ml Miniforge3/24.11.3-0
   export CONDA_PKG_DIRS=/proj/hpc-python-uppmax/$USER
   export CONDA_ENVS_PATH=/proj/hpc-python-uppmax/$USER
   conda create --prefix $CONDA_ENVS_PATH/spyder-env python=3.12 spyder -c conda-forge
   source activate spyder-env
   # A prompt "(/path-to/spyder-env/)" should show up
   # double-check we are using python from the Conda environment!
   which python  # should point to the conda environment!
   python -V     # should give python version 3.12.X
  • Let’s install packages that we need.

   conda install matplotlib pandas seaborn xarray dask numba
  • Check that the above packages are there by conda list.

We will put requirements files in the course project folder that you can build from in latter lessons

  • These will cover

    • TensorFlow

    • PyTorch

We will put requirements files in the course project folder that you can build from in latter lessons

  • These will cover

    • TensorFlow

    • PyTorch

    • numba

(Optional) Exercise 3: Install package with venv

  • Choose a track below

  • Bianca users could follow

    • (attend or cover the Bianca intermediate course <https://docs.uppmax.uu.se/courses_workshops/bianca_intermediate/>__ yourself)

  • Confirm package is absent

  • Create environment in your user’s folder in the course project

  • Activate environment

  • Confirm package is absent

  • Install package in isolated environment

  • Confirm package is now present

  • Deactivate environment

  • Confirm package is now absent again

  • Start in folder /proj/spring-courses-naiss/$USER

  • Follow the tutorial at Python <https://www.nsc.liu.se/software/python/>_: scroll down to “More on Python virtual environments (venvs)”

  • Start in folder /cfs/klemming/projects/snic/spring-courses-naiss/$USER

  • Follow the tutorial at Virtual environment with venv https://pdc-support.github.io/pdc-intro/#165

   $ module load Python/3.12.3-GCCcore-13.3.0
   $ python -m venv --system-site-packages /proj/hpc-python-uppmax/$USER/Example
   $ source /proj/hpc-python-uppmax/$USER/Example/bin/activate

“Example” is the name of the virtual environment. The directory “Example” is created in the present working directory. The -m flag makes sure that you use the libraries from the python version you are using.

   $ module load GCC/12.3.0 Python/3.11.3
   $ python -m venv /proj/nobackup/spring-courses/$USER/Example
   $ source /proj/nobackup/spring-courses/$USER/Example/bin/activate

“Example” is the name of the virtual environment. You can name it whatever you want. The directory “Example” is created in the present working directory.

   module load GCC/12.3.0 Python/3.11.3
   python -m venv --system-site-packages /path/to/your/project/$USER/Example
   source /path/to/your/project/<user-dir>/Example/bin/activate

“Example” is the name of the virtual environment. You can name it whatever you want. The directory “Example” is created in the present working directory.

  • Note that your prompt is changing to start with (Example) to show that you are within an environment.

  • Install your packages with pip. While not always needed, it is often a good idea to give the correct versions you want, to ensure compatibility with other packages you use. This example assumes your venv is activated:

   (Example) $ pip install --no-cache-dir --no-build-isolation numpy matplotlib
  • Deactivate the venv.

   (Example) $ deactivate
  • Every time you need the tools available in the virtual environment you activate it as above (after also loading the modules).

   $ source /proj/<your-project-id>/<your-dir>/Example/bin/activate

(optional) Exercise 4: like 3, but for Conda

Let’s make an installation with the latest bug fix version of Python 3.12 and compatible numpy and matplotlib in a conda environment

  • Activate environment

  • Confirm package is absent

  • Install package in isolated environment: numpy + matplotlib

  • Confirm package is now present

  • Deactivate environment

  • Confirm package is now absent again

   module load Miniforge/24.7.1-2-hpc1
   export CONDA_PKG_DIRS=/proj/spring-courses-naiss/users/$USER
   export CONDA_ENVS_PATH=/proj/spring-courses-naiss/users/$USER
   mamba create --prefix=$CONDA_ENVS_PATH/examplepython=3.12 example
   mamba activate example
   # A prompt "(/path-to/example)" should show up
   # double-check we are using python from the Conda environment!
   which python  # should point to the conda environment!
   python -V     # should give python version 3.12.X
   ml PDC/24.11
   ml miniconda3/25.3.1-1-cpeGNU-24.11
   export CONDA_ENVS_PATH="/cfs/klemming/projects/supr/spring-courses-naiss/$USER/" #only needed once per session
   export CONDA_PKG_DIRS="/cfs/klemming/projects/supr/spring-courses-naiss/$USER/" #only needed once per session
   conda create --prefix $CONDA_ENVS_PATH/example python=3.12
   source activate example
   # A prompt "(/path-to/example)" should show up
   # double-check we are using python from the Conda environment!
   which python  # should point to the conda environment!
   python -V     # should give python version 3.12.X
   ml Miniforge3/24.1.2-0
   export CONDA_ENVS_PATH="/path/to/your/project/$USER/" #only needed once per session
   export CONDA_PKG_DIRS="/path/to/your/project/$USER/" #only needed once per session
   conda create --prefix $CONDA_ENVS_PATH/example python=3.12
   conda activate example
   # A prompt "(/path-to/example)" should show up
   # double-check we are using python from the Conda environment!
   which python  # should point to the conda environment!
   python -V     # should give python version 3.12.X
    ml conda
    export CONDA_PKG_DIRS=/proj/hpc-python-uppmax/$USER
    export CONDA_ENVS_PATH=/proj/hpc-python-uppmax/$USER
    conda create --prefix $CONDA_ENVS_PATH/example python=3.12 -c conda-forge
    source activate example
    # A prompt "(/path-to/example/)" should show up
    # double-check we are using python from the Conda environment!
    which python  # should point to the conda environment!
    python -V     # should give python version 3.12.X
   ml Miniforge3/24.11.3-0
   export CONDA_PKG_DIRS=/proj/hpc-python-uppmax/$USER  #only needed once per session
   export CONDA_ENVS_PATH=/proj/hpc-python-uppmax/$USER  #only needed once per session
   conda create --prefix $CONDA_ENVS_PATH/example python=3.12 -c conda-forge
   source activate example
   # A prompt "(/path-to/example)" should show up
   # double-check we are using python from the Conda environment!
   which python  # should point to the conda environment!
   python -V     # should give python version 3.12.X

Skip this exercise

  • Let’s install packages that we need.

   conda install matplotlib numpy
  • Check that the above packages are there by conda list.

(optional) 5. Make a test environment and spread (venv)

Read here

  1. make a virtual environment with the name venv1. Do not include packages from the the loaded module(s)

  2. activate

  3. install matplotlib

  4. make a requirements file of the content

  5. deactivate

  6. make another virtual environment with the name venv2

  7. activate that

  8. install with the aid of the requirements file

  9. check the content

  10. open python shell from command line and try to import matplotlib

  11. exit python

  12. deactivate

(optional) Exercise 5b. Make a test environment (conda)

Principle

  • Start in an environment created above

  • Export the settings:

   conda env export > environment.yml
  • Look at the resulting file!

  • Create a copy of the environment but called another way to not overwrite the original!

    • Usually someone else use your file or you, yourself, on another computer.

   conda env create -f environment.yml -p /lunarc/nobackup/projects/lu2025-17-52/bjornc/test

Summary

Keypoints

  • With a virtual environment you can tailor an environment with specific versions for Python and packages, not interfering with other installed python versions and packages.

  • Make it for each project you have for reproducibility.

  • There are different tools to create virtual environments.

    • venv, most straight-forward and available at all HPC centers. Recommended

      • only supports Python packages

    • conda, only recommended at some clusters

      • supports more and is a bit more reliable

      • do not use together with Python modules

      • install in project folder due to many files.

Summary of the workflows

See also