Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

bibliothèque standard

un grand nombre d’outils installés d’office
pour des tâches très variées voir liste complète

cette boite à outils est exposée au travers de modules
que l’on peut charger dans son appli grâce au mot-clé import

import

module
3.141592653589793

autres formes

3.141592653589793
-1.0

installation de librairies tierces

si on a besoin d’installer un module
qui ne fait pas partie de la bibliothèque standard :

Collecting nbautoeval
  Downloading nbautoeval-1.8.0-py3-none-any.whl.metadata (5.0 kB)
Requirement already satisfied: ipython in /__w/numerique/numerique/venv/lib/python3.14/site-packages (from nbautoeval) (9.17.1)
Requirement already satisfied: ipywidgets in /__w/numerique/numerique/venv/lib/python3.14/site-packages (from nbautoeval) (8.1.9)
Collecting myst-parser (from nbautoeval)
  Downloading myst_parser-5.1.0-py3-none-any.whl.metadata (5.6 kB)
Requirement already satisfied: numpy in /__w/numerique/numerique/venv/lib/python3.14/site-packages (from nbautoeval) (2.5.3)
Requirement already satisfied: pyyaml in /__w/numerique/numerique/venv/lib/python3.14/site-packages (from nbautoeval) (6.0.3)
Requirement already satisfied: ipython-pygments-lexers>=1.0.0 in /__w/numerique/numerique/venv/lib/python3.14/site-packages (from ipython->nbautoeval) (1.1.1)
Requirement already satisfied: jedi>=0.18.2 in /__w/numerique/numerique/venv/lib/python3.14/site-packages (from ipython->nbautoeval) (0.20.0)
Requirement already satisfied: matplotlib-inline>=0.1.6 in /__w/numerique/numerique/venv/lib/python3.14/site-packages (from ipython->nbautoeval) (0.2.2)
Requirement already satisfied: pexpect>4.6 in /__w/numerique/numerique/venv/lib/python3.14/site-packages (from ipython->nbautoeval) (4.9.0)
Requirement already satisfied: prompt_toolkit<3.1.0,>=3.0.41 in /__w/numerique/numerique/venv/lib/python3.14/site-packages (from ipython->nbautoeval) (3.0.53)
Requirement already satisfied: psutil>=7 in /__w/numerique/numerique/venv/lib/python3.14/site-packages (from ipython->nbautoeval) (7.2.2)
Requirement already satisfied: pygments>=2.14.0 in /__w/numerique/numerique/venv/lib/python3.14/site-packages (from ipython->nbautoeval) (2.21.0)
Requirement already satisfied: stack_data>=0.6.0 in /__w/numerique/numerique/venv/lib/python3.14/site-packages (from ipython->nbautoeval) (0.6.3)
Requirement already satisfied: traitlets>=5.13.0 in /__w/numerique/numerique/venv/lib/python3.14/site-packages (from ipython->nbautoeval) (5.16.1)
Requirement already satisfied: wcwidth>=0.1.4 in /__w/numerique/numerique/venv/lib/python3.14/site-packages (from prompt_toolkit<3.1.0,>=3.0.41->ipython->nbautoeval) (0.9.2)
Requirement already satisfied: parso<0.9.0,>=0.8.6 in /__w/numerique/numerique/venv/lib/python3.14/site-packages (from jedi>=0.18.2->ipython->nbautoeval) (0.8.7)
Requirement already satisfied: ptyprocess>=0.5 in /__w/numerique/numerique/venv/lib/python3.14/site-packages (from pexpect>4.6->ipython->nbautoeval) (0.7.0)
Requirement already satisfied: executing>=1.2.0 in /__w/numerique/numerique/venv/lib/python3.14/site-packages (from stack_data>=0.6.0->ipython->nbautoeval) (2.2.1)
Requirement already satisfied: asttokens>=2.1.0 in /__w/numerique/numerique/venv/lib/python3.14/site-packages (from stack_data>=0.6.0->ipython->nbautoeval) (3.0.2)
Requirement already satisfied: pure-eval in /__w/numerique/numerique/venv/lib/python3.14/site-packages (from stack_data>=0.6.0->ipython->nbautoeval) (0.2.4)
Requirement already satisfied: comm>=0.1.3 in /__w/numerique/numerique/venv/lib/python3.14/site-packages (from ipywidgets->nbautoeval) (0.2.3)
Requirement already satisfied: widgetsnbextension~=4.0.16 in /__w/numerique/numerique/venv/lib/python3.14/site-packages (from ipywidgets->nbautoeval) (4.0.16)
Requirement already satisfied: jupyterlab_widgets~=3.0.17 in /__w/numerique/numerique/venv/lib/python3.14/site-packages (from ipywidgets->nbautoeval) (3.0.17)
Collecting docutils<0.23,>=0.20 (from myst-parser->nbautoeval)
  Downloading docutils-0.22.4-py3-none-any.whl.metadata (15 kB)
Requirement already satisfied: jinja2 in /__w/numerique/numerique/venv/lib/python3.14/site-packages (from myst-parser->nbautoeval) (3.1.6)
Requirement already satisfied: markdown-it-py~=4.2 in /__w/numerique/numerique/venv/lib/python3.14/site-packages (from myst-parser->nbautoeval) (4.2.0)
Requirement already satisfied: mdit-py-plugins>=0.6.1,~=0.6 in /__w/numerique/numerique/venv/lib/python3.14/site-packages (from myst-parser->nbautoeval) (0.6.1)
Collecting sphinx<10,>=8 (from myst-parser->nbautoeval)
  Downloading sphinx-9.1.0-py3-none-any.whl.metadata (5.8 kB)
Requirement already satisfied: mdurl~=0.1 in /__w/numerique/numerique/venv/lib/python3.14/site-packages (from markdown-it-py~=4.2->myst-parser->nbautoeval) (0.1.2)
Collecting sphinxcontrib-applehelp>=1.0.7 (from sphinx<10,>=8->myst-parser->nbautoeval)
  Downloading sphinxcontrib_applehelp-2.0.0-py3-none-any.whl.metadata (2.3 kB)
Collecting sphinxcontrib-devhelp>=1.0.6 (from sphinx<10,>=8->myst-parser->nbautoeval)
  Downloading sphinxcontrib_devhelp-2.0.0-py3-none-any.whl.metadata (2.3 kB)
Collecting sphinxcontrib-htmlhelp>=2.0.6 (from sphinx<10,>=8->myst-parser->nbautoeval)
  Downloading sphinxcontrib_htmlhelp-2.1.0-py3-none-any.whl.metadata (2.3 kB)
Collecting sphinxcontrib-jsmath>=1.0.1 (from sphinx<10,>=8->myst-parser->nbautoeval)
  Downloading sphinxcontrib_jsmath-1.0.1-py2.py3-none-any.whl.metadata (1.4 kB)
Collecting sphinxcontrib-qthelp>=1.0.6 (from sphinx<10,>=8->myst-parser->nbautoeval)
  Downloading sphinxcontrib_qthelp-2.0.0-py3-none-any.whl.metadata (2.3 kB)
Collecting sphinxcontrib-serializinghtml>=1.1.9 (from sphinx<10,>=8->myst-parser->nbautoeval)
  Downloading sphinxcontrib_serializinghtml-2.0.0-py3-none-any.whl.metadata (2.4 kB)
Collecting snowballstemmer>=2.2 (from sphinx<10,>=8->myst-parser->nbautoeval)
  Downloading snowballstemmer-3.1.1-py3-none-any.whl.metadata (7.9 kB)
Requirement already satisfied: babel>=2.13 in /__w/numerique/numerique/venv/lib/python3.14/site-packages (from sphinx<10,>=8->myst-parser->nbautoeval) (2.18.0)
Collecting alabaster>=0.7.14 (from sphinx<10,>=8->myst-parser->nbautoeval)
  Downloading alabaster-1.0.0-py3-none-any.whl.metadata (2.8 kB)
Collecting imagesize>=1.3 (from sphinx<10,>=8->myst-parser->nbautoeval)
  Downloading imagesize-2.0.1-py3-none-any.whl.metadata (7.0 kB)
Requirement already satisfied: requests>=2.30.0 in /__w/numerique/numerique/venv/lib/python3.14/site-packages (from sphinx<10,>=8->myst-parser->nbautoeval) (2.34.2)
Collecting roman-numerals>=1.0.0 (from sphinx<10,>=8->myst-parser->nbautoeval)
  Downloading roman_numerals-4.1.0-py3-none-any.whl.metadata (3.3 kB)
Requirement already satisfied: packaging>=23.0 in /__w/numerique/numerique/venv/lib/python3.14/site-packages (from sphinx<10,>=8->myst-parser->nbautoeval) (26.3)
Requirement already satisfied: MarkupSafe>=2.0 in /__w/numerique/numerique/venv/lib/python3.14/site-packages (from jinja2->myst-parser->nbautoeval) (3.0.4)
Requirement already satisfied: charset_normalizer<4,>=2 in /__w/numerique/numerique/venv/lib/python3.14/site-packages (from requests>=2.30.0->sphinx<10,>=8->myst-parser->nbautoeval) (3.5.2)
Requirement already satisfied: idna<4,>=2.5 in /__w/numerique/numerique/venv/lib/python3.14/site-packages (from requests>=2.30.0->sphinx<10,>=8->myst-parser->nbautoeval) (3.20)
Requirement already satisfied: urllib3<3,>=1.26 in /__w/numerique/numerique/venv/lib/python3.14/site-packages (from requests>=2.30.0->sphinx<10,>=8->myst-parser->nbautoeval) (2.8.0)
Requirement already satisfied: certifi>=2023.5.7 in /__w/numerique/numerique/venv/lib/python3.14/site-packages (from requests>=2.30.0->sphinx<10,>=8->myst-parser->nbautoeval) (2026.7.22)
Downloading nbautoeval-1.8.0-py3-none-any.whl (47 kB)
Downloading myst_parser-5.1.0-py3-none-any.whl (85 kB)
Downloading docutils-0.22.4-py3-none-any.whl (633 kB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 0.0/633.2 kB ? eta -:--:--
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 633.2/633.2 kB 4.8 MB/s eta 0:00:00
Downloading sphinx-9.1.0-py3-none-any.whl (3.9 MB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 0.0/3.9 MB ? eta -:--:--
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 3.9/3.9 MB 19.1 MB/s eta 0:00:00
Downloading alabaster-1.0.0-py3-none-any.whl (13 kB)
Downloading imagesize-2.0.1-py3-none-any.whl (14 kB)
Downloading roman_numerals-4.1.0-py3-none-any.whl (7.7 kB)
Downloading snowballstemmer-3.1.1-py3-none-any.whl (104 kB)
Downloading sphinxcontrib_applehelp-2.0.0-py3-none-any.whl (119 kB)
Downloading sphinxcontrib_devhelp-2.0.0-py3-none-any.whl (82 kB)
Downloading sphinxcontrib_htmlhelp-2.1.0-py3-none-any.whl (98 kB)
Downloading sphinxcontrib_jsmath-1.0.1-py2.py3-none-any.whl (5.1 kB)
Downloading sphinxcontrib_qthelp-2.0.0-py3-none-any.whl (88 kB)
Downloading sphinxcontrib_serializinghtml-2.0.0-py3-none-any.whl (92 kB)
Installing collected packages: sphinxcontrib-serializinghtml, sphinxcontrib-qthelp, sphinxcontrib-jsmath, sphinxcontrib-htmlhelp, sphinxcontrib-devhelp, sphinxcontrib-applehelp, snowballstemmer, roman-numerals, imagesize, docutils, alabaster, sphinx, myst-parser, nbautoeval
   ━━╸━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━  1/14 [sphinxcontrib-qthelp]
   ━━━━━━━━━━━╺━━━━━━━━━━━━━━━━━━━━━━━━━━━━  4/14 [sphinxcontrib-devhelp]
   ━━━━━━━━━━━━━━━━━╺━━━━━━━━━━━━━━━━━━━━━━  6/14 [snowballstemmer]
   ━━━━━━━━━━━━━━━━━╺━━━━━━━━━━━━━━━━━━━━━━  6/14 [snowballstemmer]
   ━━━━━━━━━━━━━━━━━━━━╺━━━━━━━━━━━━━━━━━━━  7/14 [roman-numerals]
   ━━━━━━━━━━━━━━━━━━━━━━━━━╸━━━━━━━━━━━━━━  9/14 [docutils]
   ━━━━━━━━━━━━━━━━━━━━━━━━━╸━━━━━━━━━━━━━━  9/14 [docutils]
   ━━━━━━━━━━━━━━━━━━━━━━━━━╸━━━━━━━━━━━━━━  9/14 [docutils]
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━╸━━━━━━━━━━━ 10/14 [alabaster]
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╺━━━━━━━━ 11/14 [sphinx]
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╺━━━━━━━━ 11/14 [sphinx]
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╺━━━━━━━━ 11/14 [sphinx]
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╺━━━━━━━━ 11/14 [sphinx]
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╺━━━━━━━━ 11/14 [sphinx]
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╺━━━━━━━━ 11/14 [sphinx]
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╺━━━━━ 12/14 [myst-parser]
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 14/14 [nbautoeval]
Successfully installed alabaster-1.0.0 docutils-0.22.4 imagesize-2.0.1 myst-parser-5.1.0 nbautoeval-1.8.0 roman-numerals-4.1.0 snowballstemmer-3.1.1 sphinx-9.1.0 sphinxcontrib-applehelp-2.0.0 sphinxcontrib-devhelp-2.0.0 sphinxcontrib-htmlhelp-2.1.0 sphinxcontrib-jsmath-1.0.1 sphinxcontrib-qthelp-2.0.0 sphinxcontrib-serializinghtml-2.0.0

c’est quoi un module ?

un module est un objet Python qui correspond à un fichier (ou répertoire) source
depuis cet objet vous pouvez accéder à des attributs
avec la notation module.attribut
(qui est btw la même notion que, par ex., str.capitalize)
le module a autant d’attributs que d’objets globaux dans le code source
dans le cas d’un répertoire les attributs référencent d’autres modules

GLOBALE = 100

def spam(data):
    print(f"dans mod.spam, data={data}")
['GLOBALE', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'spam']
['GLOBALE', 'spam']

notion de point d’entrée

votre programme Python est toujours exécuté par un interpréteur
qui “commence” quelque part: c’est le point d’entrée

si vous lancez

$ python3 foo.py

le point d’entrée dans ce cas est (le module correspondant à) foo.py

recherche des modules

Python recherche les modules dans plusieurs d’endroits (répertoires)

conseil : évitez de bidouiller PYTHONPATH

organisation de votre code

cela signifie que pour commencer,
on peut sans souci couper son code en fichiers
et les mettre tous dans le même répertoire

c’est une pratique courante et recommandée
il faut apprendre à découper
notamment pour augmenter la réutilisabilité

librairies utiles

liste largement non exhaustive

module pathlib

fait partie de la librairie standard
permet de faire des calculs sur les fichiers

module random

une fois que vous avez le nom du module,
il vous suffit de consulter la doc complète
pour cela taper dans google python random module
(nous reviendrons sur ce module lors de l’étude des librairies numériques)

0.30417164438215794
9

module requests

télécharger du contenu depuis une URL
accéder à l’entête http plus flexible que l’équivalent dans la librairie standard urllib2

code de retour HTTP: 410
str
'{"message":"Hello there, wayfaring stranger. If you’re reading this then you probably didn’t see our blog post a couple '

module json

le cas qu’on vient de voir est très fréquent
JSON est un format texte (compatible réseau donc Internet)
mais qui conserve un minimum de structure : permet de transmettre listes et dictionnaires

dict
message
	Hello there, wayfari...
documentation_url
	https://docs.github....

gestion de fichiers

with open... ouvre le fichier tutu.txt en écriture "w"
et crée la variable writer de type File
writer n’est visible que dans le with
print(..., file=writer) écrit dans ce fichier
with ferme le fichier à la sortie
(nous allons maintenant lire ce fichier)

fichiers - suite

i=0
i=1
i=2
i=3

sans préciser le mode d’ouverture
open ouvre le fichier en lecture
l’objet reader est itérable

la variable line contient une fin de ligne
pas besoin que print en rajoute une

exercice

écrire une fonction json_random() qui retourne :

---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
Cell In[23], line 1
----> 1 from check_random_json import check_random_json
      2 
      3 check_random_json(random_json)

ModuleNotFoundError: No module named 'check_random_json'