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.

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

Parfois, l’installation peut échouer. Vérifier que la librairie est bien compatible avec la version de Python.

Exemple avec ktrain

Par exemple, à l’instant où nous avons écrit ces lignes, la librairie ktrain échoue avec Python 3.11.2.

$ ml compiler/python/3.11.2 
$ python3 --version
Python 3.11.2
$ pip3 install --user ktrain
Collecting ktrain
  Using cached ktrain-0.37.2.tar.gz (25.3 MB)
...
src/cchardet/_cchardet.cpp:196:12: fatal error: longintrepr.h: No such file or directory

Pour l’installer, il faut utiliser une version de Python plus ancienne. Par exemple, Python 3.9.16

$ ml load compiler/python/3.9.16
$ python3 --version
Python 3.9.16
$ pip install --upgrade pip
$ pip3 install --upgrade --user ktrain 

Cependant, il faut aussi s’assurer que l’import fonctionne.

$ python3
Python 3.9.16 (main, Mar  9 2023, 23:53:30) 
[GCC 12.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import ktrain
/.../.local/lib/python3.9/site-packages/ktrain/imports.py:59: UserWarning: TensorFlow is not installed and will be needed if training neural networks, but non-TensorFlow features in ktrain can still be used. See https://github.com/amaiya/ktrain/blob/master/README.md
  warnings.warn(TF_WARNING)
None of PyTorch, TensorFlow >= 2.0, or Flax have been found. Models won't be available and only tokenizers, configuration and file/data utilities can be used.
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/.../.local/lib/python3.9/site-packages/ktrain/__init__.py", line 2, in <module>
    from . import utils as U
  File "/.../.local/lib/python3.9/site-packages/ktrain/utils.py", line 30, in <module>
    if version.parse(tf.__version__) < version.parse("2.11")
AttributeError: 'NoneType' object has no attribute '__version__'

Dans notre exemple, l’erreur se corrige en installant au moins le paquet tensorflow.

$ pip install --upgrade torch
$ pip install --upgrade tensorflow 

Et maintenant

$ python3
Python 3.9.16 (main, Mar  9 2023, 23:53:30) 
[GCC 12.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import ktrain
>>> quit()  

Example with pytorch

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
ml compiler/python/3.11.2
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 peut rajouter l’option –user dans la commande pip3.
  2. Les librairies sont placés dans votre répertoire .local.