Python 3

Lien pour mise en place de l’environnement python !

Lancement de python3

  • Pour utiliser Python, tapez la ligne de commande « python3 ».
$ python3
Python 3.6.8 (default, Sep 13 2022, 09:02:49) 
[GCC 8.5.0 20210514 (Red Hat 8.5.0-10)] on linux
Type "help", "copyright", "credits" or "license" for more information.
  • Vous pouvez utiliser une version plus récente de python3 en chargeant un module.
$ ml compiler/python/3.11.0 
$ python3
Python 3.11.0 (main, Dec  1 2022, 09:03:09) [GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.

Chargement d’une librairie

  • Le chargement d’une librairie se fait avec la commande import
$ python3
Python 3.6.8 (default, Sep 13 2022, 09:02:49) 
[GCC 8.5.0 20210514 (Red Hat 8.5.0-10)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pandas
  • Une librairie peut être installée dans une version de Python mais pas dans une autre.
$ ml compiler/python/3.11.0 
$ python3
Python 3.11.0 (main, Dec  1 2022, 09:03:09) [GCC 9.4.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'

Dans cet exemple, la librairie pandas est bien installée dans la version 3.6.8 mais pas dans la version 3.11.0.

Installation d’une librairie

En tant de que simple utilisateur, possibilité d’installer une librairie python.

Actuellement, le chargement d’une librairie installée échoue. Ce problème est en cours de résolution

Exemple avec pandas

$ ml compiler/python/3.11.0 
$ pip3.11 install --no-cache-dir --upgrade --user pandas
Collecting pandas
  Downloading pandas-1.5.3-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (12.0 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 12.0/12.0 MB 122.9 MB/s eta 0:00:00
Collecting python-dateutil>=2.8.1
  Downloading python_dateutil-2.8.2-py2.py3-none-any.whl (247 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 247.7/247.7 kB 281.3 MB/s eta 0:00:00
Collecting pytz>=2020.1
  Downloading pytz-2022.7.1-py2.py3-none-any.whl (499 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 499.4/499.4 kB 300.8 MB/s eta 0:00:00
Collecting numpy>=1.21.0
  Downloading numpy-1.24.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (17.3 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 17.3/17.3 MB 150.2 MB/s eta 0:00:00
Collecting six>=1.5
  Downloading six-1.16.0-py2.py3-none-any.whl (11 kB)
Installing collected packages: pytz, six, numpy, python-dateutil, pandas
Successfully installed numpy-1.24.2 pandas-1.5.3 python-dateutil-2.8.2 pytz-2022.7.1 six-1.16.0
$ python3
Python 3.11.0 (main, Dec  1 2022, 09:03:09) [GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pandas

Exemple avec pytorch (MatriCS 1.0)

Le script ci-après installe en local pytorch (Attention! Le script le désinstalle aussi une fois la tâche terminée).
Le script de test my_torch.py se trouve à la page suivante : https://pytorch.org/get-started/locally/

Script de test

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

Script du lancement de job pytorch

Installation pytorch, exécution du programme puis désinstallation de 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

Remarques :

  1. Pour installer une librairie python sur son compte en local en tant qu’utilisateur, on rajoute l’option –user dans la commande pip3.
  2. Les librairies sont placés dans votre répertoire .local.