Python 3

Launching python 3

For using Python, typ the command "python3"
$ python3
Python 3.6.8 (default, Nov 16 2020, 16:55:22) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux
Type "help", "copyright", "credits" or "license" for more information.
You can use a more recent version of Python with the loading of a Python module. For example, 
$ module load programming/python3/3.10.4
$ python3
Python 3.10.4 (main, Sep 13 2022, 15:31:07) [GCC 8.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.

Loading a library.

$ python3
Python 3.6.8 (default, Nov 16 2020, 16:55:22) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pandas
A library can be installed in a Python version but not in another.
$ module load programming/python3/3.10.4
$ python3
Python 3.10.4 (main, Sep 13 2022, 15:31:07) [GCC 8.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pandas
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'pandas'

The library called pandas is well installed in the version 3.6.8 but not into the version 3.10.4.

Installation of a library

As a user, possibility to install a Python library.

Example with pytorch

The following script installs locally pytorch (Be careful! The script uninstalls it once the task is done, too).
The script of test my_torch.py can be found at : https://pytorch.org/get-started/locally/

Script of test

from __future__ import print_function
import torch
x = torch.rand(5, 3)
print(x)

Script for launching the job pytorch

Installation pytorch, running the program then uninstallation of pytorch
#!/bin/sh
#SBATCH --job-name=pytorch
#SBATCH --partition=normal
#SBATCH --output=job-%j.out
#SBATCH --nodes=1
mkdir -p ~/tmp
export TMPDIR=~/tmp
echo "--- INSTALL PYTHON LIBRARIES ---"
pip3 install --no-cache-dir --upgrade --user torch torchvision
echo "--- LAUNCH PROGRAM ---"
date
time python3 my_torch.py
date
echo "--- DESINSTALLATION ---" /usr/bin/yes | pip3 uninstall torchvision
/usr/bin/yes | pip3 uninstall torch

Notes :

  1. For installing locally a python library on his/her account as a simple user, we add the option –user in the command pip3.
  2. The libraries are put into your hidden folder .local.