Bonjour

j'ai pu constater que certains ne savaient pas quel environnement python ils utilisaient.
Une commande qui devrait nous être utile :
En fonction de l'endroit où nous la lançons (terminal, ide, dossier particulier), nous pouvons avoir un environnement python différent (python différent ou modules différents)

Par exemple, avec ce retour ...from /home/patrick/.pyenv/versions/3.11.2/lib/python3.11/site-packages/pip (python 3.11), je peux voir que j'utilise un python personnel et non celui du système.

Note: dans tout script python, nous avons sys.base_exec_prefix-----------

En complément, hier, j'ai visité quelques pages de la documentation que je ne connaissais pas, et j'en ai tiré un petit script.
Script à installer dans son PATH, qui va donner des infos sur son environnement (virtuel ou non). Il est possible de l'étendre pour donner plus de détails.

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/usr/bin/env python
"""
https://docs.python.org/fr/3/library/importlib.metadata.html
"""
import platform
import sys
from importlib import metadata as meta
 
 
def _hi(string: str, use: bool = True) -> str:
    if not use:
        return string
    return f"\33[32m{string}\33[0m"
 
eps = meta.entry_points()
pips = sorted(set(p for l in meta.packages_distributions().values() for p in l))
 
for pkg in pips:
    pkg = meta.distribution(pkg)
    if not pkg:
        continue
 
    # voir pour les champs : https://packaging.python.org/en/latest/specifications/core-metadata/#core-metadata
    name = f"{pkg.name:20}"
    print(f"{_hi(name)}{pkg.version:<11} {pkg.metadata.get('Summary', '')}  ")
    if home := pkg.metadata['Home-page']:
        print(" "*31, f"{home}")
 
    if ep := eps.select(name=pkg.name):
        #Uniquement les paquets avec un fichier __main__.py
        print(" "*31, "# est une application")
 
    if files := [True for p in meta.files(pkg.name) if p.suffix == ".py"]:
        print(" "*31, len(files), "fichiers python")
 
    if requests := meta.requires(pkg.name):
        sani = lambda x: ''.join([c if c not in ('[', '>', '=', '~', ';') else ' ' for c in x]).split()[0]
        requests = sorted(set(sani(r) for r in requests))
        # info intéressante mais casse cette mise en page
        # print(" "*31, f"Peut utiliser: {tuple(requests) if len(requests)>1 else requests[0]}")
 
    print()
 
print()
print("# ", len(pips), "paquets")
pip = meta.distribution("pip")
print("#  Environnement Python utilisé:", pip._path.parent.parent)
print("#  Environnement Python utilisé:", sys.base_exec_prefix)
print("#  Interpréteur Python :", sys.executable)
Un exemple de sortie :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
pyflakes            3.0.1       passive checker of Python programs  
                                https://github.com/PyCQA/pyflakes
                                # est une application
                                21 fichiers python
...
 
urllib3             1.26.15     HTTP library with thread-safe connection pooling, file post, and more.  
                                https://urllib3.readthedocs.io/
                                38 fichiers python
 
wrapt               1.15.0      Module for decorators, wrappers and monkey patching.  
                                https://github.com/GrahamDumpleton/wrapt
                                5 fichiers python
 
wsproto             1.2.0       WebSockets state-machine based protocol implementation  
                                https://github.com/python-hyper/wsproto/
                                8 fichiers python
 
 
#  40 paquets
#  Python: 3.11.2
#  Environnement Python utilisé: /home/patrick/.pyenv/versions/3.11.2/lib/python3.11
#  Environnement Python utilisé: /home/patrick/.pyenv/versions/3.11.2
#  Interpréteur Python : /home/patrick/.pyenv/versions/3.11.2/bin/python
Toujours sur le même machine, mais dans un autre environnement
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
#  257 paquets
#  Python: 3.10.10
#  Environnement Python utilisé: /usr/lib/python3.10
#  Environnement Python utilisé: /usr
#  Interpréteur Python : /usr/bin/python