Load and run python

At UPPMAX, HPC2N, and LUNARC we call the applications available via the module system modules.

Most HPC centres in Sweden is using the same or similar module system for their software. The difference lies in which modules are installed and their versions/naming. The general examples below will be the same for all/most HPC centres in Sweden - the specific names for the modules will vary a little.

Objectives

  • Learn how to load Python

  • Learn how to run Python scripts and start the Python command line

Warning

  • Note that the module systems at UPPMAX, HPC2N, and LUNARC are slightly different.

  • While all modules at UPPMAX not directly related to bio-informatics are shown by ml avail, some modules at HPC2N and LUNARC are hidden until one has loaded a prerequisite like the compiler GCC.

  • Thus, you need to use module spider or ml spider to see all modules at HPC2N and LUNARC, and ml avail for those available to load given your currently loaded prerequisites.

  • For reproducibility reasons, you should always load a specific version of a module instead of just the default version

  • Many modules have prerequisite modules which needs to be loaded first (at HPC2N this is also the case for the Python modules). When doing module spider <module>/<version> you will get a list of which other modules needs to be loaded first

Check for Python versions

Type-Along

Checking for Python versions

Check all available Python versions with:

$ module avail python

Note

We will use Python 3.11.x in this course!

Load a Python module

For reproducibility, we recommend ALWAYS loading a specific module instead of using the default version!

For this course, we recommend using Python 3.11.x, at UPPMAX (3.11.8), at HPC2N (3.11.3), and at LUNARC (3.11.3).

Type-Along

Loading a Python module. Here Python 3.11.x

Go back and check which Python modules were available. To load version 3.11.8, do:

$ module load python/3.11.8

Note: Lowercase p. For short, you can also use:

$ ml python/3.11.8

Warning

  • UPPMAX: Don’t use system-installed python (2.7.5)

  • UPPMAX: Don’t use system installed python3 (3.6.8)

  • HPC2N: Don’t use system-installed python (2.7.18)

  • HPC2N: Don’t use system-installed python3 (3.8.10)

  • LUNARC: Don’t use system-installed python/python3 (3.9.18)

  • ALWAYS use python module

Why are there both Python/2.X.Y and Python/3.Z.W modules?

Some existing software might use Python2 and some will use Python3. Some of the Python packages have both Python2 and Python3 versions. Check what your software as well as the installed modules need when you pick!

UPPMAX: Why are there both python/3.X.Y and python3/3.X.Y modules?

Sometimes existing software might use python2 and there’s nothing you can do about that. In pipelines and other toolchains the different tools may together require both python2 and python3. Here’s how you handle that situation:

  • You can run two python modules at the same time if ONE of the module is python/2.X.Y and the other module is python3/3.X.Y (not python/3.X.Y).

LUNARC: Are python and python3 equivalent, or does the former load Python/2.X.Y?

The answer depends on which module is loaded. If Python/3.X.Y is loaded, then python is just an alias for python3 and it will start the same command line. However, if Python/2.7.X is loaded, then python will start the Python/2.7.X command line while python3 will start the system version (3.9.18). If you load Python/2.7.X and then try to load Python/3.X.Y as well, or vice-versa, the most recently loaded Python version will replace anything loaded prior, and all dependencies will be upgraded or downgraded to match. Only the system’s Python/3.X.Y version can be run at the same time as a version of Python/2.7.X.

Run

Run Python script

You can run a python script in the shell like this:

$ python example.py

or, if you loaded a python3 module, you can use:

$ python3 example.py

since python is a symbolic link to python3 in this case.

NOTE: only run jobs that are short and/or do not use a lot of resources from the command line. Otherwise use the batch system!

Note

Real cases will be tested in the batch session (https://uppmax.github.io/R-python-julia-matlab-HPC/python/batchPython.html).

Run an interactive Python shell

For more interactiveness you can run Ipython.

Type-Along

Starting ipython

NOTE: remember to load a python module first. Then start IPython from the terminal

$ ipython

or

$ ipython3

UPPMAX has also jupyter-notebook installed and available from the loaded Python module. Start with

$ jupyter-notebook

Examples (Try them out! Remember to load suitable modules first!)

Python

$ python
Python 3.11.3 (main, Oct 30 2023, 16:00:15) [GCC 12.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> a=3
>>> b=7
>>> c=a+b
>>> c
10

iPython

$ ipython
Python 3.11.3 (main, Oct 30 2023, 16:00:15) [GCC 12.3.0]
Type 'copyright', 'credits' or 'license' for more information
IPython 8.14.0 -- An enhanced Interactive Python. Type '?' for help.
In [1]: a=3
In [2]: b=7
In [3]: c=a+b
In [4]: c
Out[4]: 10
  • Exit Python or IPython with <Ctrl-D>, “quit()” or “exit()” in the python prompt

Python

>>> <Ctrl-D>
>>> quit()
>>> exit()

iPython

In [2]: <Ctrl-D>
In [12]: quit()
In [17]: exit()

Keypoints

  • Before you can run Python scripts or work in a Python shell, first load a python module and probable prerequisites

  • Start a Python shell session either with python or ipython

  • Run scripts with python <script.py>