Install packages

Objectives

  • Learn how to install a (general-purpose) Python package with pip

  • Understand limitations of this way, e.g. use cases/best practices

Introduction

There are 2 ways to install missing python packages at a HPC cluster.

  • Local installation, always available for the version of Python you had active when doing the installation
    • pip install --user [package name]

  • Isolated environment. See next session
    • virtual environents provided by python

    • conda

Normally you want reproducibility and the safe way to go is with isolated environments specific to your different projects.

Use cases of local general packages

  • Packages, missing in the loaded Python module, that would not be specific for a research project.

  • Comment: You can include the package in a virtual environment as well.

Typical workflow

  1. Load the Python module with correct version.
    • Differs among the clusters

  2. Check that the right python is used with which python3 or which python
    • Double check the version python3 -V or python -V

  3. Install with: pip install --user <package-name [version]>

    • Bit more secure (using really the loaded Python): python -m pip install --user <package-name [version]>

Versions

  • Package name can be pinned,
    • like numpy==1.26.4 (Note the double ==)

    • like numpy>1.22

    • read more

  • If not pinned you will get the latest version compatible with the python version you are using.

Installation directory

  • The package typically ends up in ~/.local/lib/python3.X
    • Example: Packages installed by both bython version 3.11.5 and 3.11.8 will go into same folder python3.11 and will be seen by both python interpreters.

Check your installed packages (and dependencies)

  • Check with pip list --user

  • It is evident that some packages have dependencies! In the example, xarray also installs numpy and pandas!

  • It is also evident that if numpy and pandas already are available, those will be used!

Exercise

(optional) Exercise 1: Install a python package you know of for an old version of Python

  • Load an older Python module (perhaps one you won’t use anymore)

  • install the python package (it may already be there but with an older version)
    • (you can always remove your local installation later if you regret it)

  • We may add a solution in a coming instance of the course