Comment obtenir la version Python

Voici comment obtenir la version Python à partir d'un programme Python en cours d'exécution et un exemple. Il existe plusieurs façons d'obtenir la version:

  1. sys.version_info
  2. platform.python_version_tuple()
  3. six

Méthode 1: sys.version_info

sys.version_info stocke la version Python de l'environnement d'exécution. Pour Python inférieur à 2.7 / 3.0, le type est «tuple», et pour 2.7 / 3.1 et versions ultérieures, le type est «nommé tuple».

Exemple d'exécution

python2.5


>>> import sys
>>> sys.version_info
(2, 5, 2, 'final', 0)

python2.7


>>> import sys
>>> sys.version_info
sys.version_info(major=2, minor=7, micro=12, releaselevel='final', serial=0)

python3.5


>>> import sys
>>> sys.version_info
sys.version_info(major=3, minor=5, micro=2, releaselevel='final', serial=0)

Reference [28.1. sys — System-specific parameters and functions — Python 2.7.11 documentation] (https://docs.python.org/2.7/library/sys.html)

sys.version_info

A tuple containing the five components of the version number: major, minor, micro, releaselevel, and serial. All values except releaselevel are integers; the release level is 'alpha', 'beta', 'candidate', or 'final'. The version_info value corresponding to the Python version 2.0 is (2, 0, 0, 'final', 0). The components can also be accessed by name, so sys.version_info[0] is equivalent to sys.version_info.major and so on.

New in version 2.0.

Changed in version 2.7: Added named component attributes

Méthode 2: platform.python_version_tuple ()

platform.python_version_tuple () obtient les informations sur le numéro de version en tant que tuple quelle que soit la version de Python.

Exemple d'exécution

>>> import platform
>>> platform.python_version_tuple()
('2', '7', '14')

python3.6


>>> import platform
>>> platform.python_version_tuple()
('3', '6', '4')

Reference 15.15. platform — Access to underlying platform’s identifying data — Python 2.7.11 documentation

New in version 2.3.

platform.python_version_tuple()

Returns the Python version as tuple (major, minor, patchlevel) of strings.

Note that unlike the Python sys.version, the returned value will always include the patchlevel (it defaults to '0').

Méthode 3: six

six peut être utilisé pour déterminer s'il s'agit de Python 2 ou Python 3 (et Python 3.4 ou supérieur).

Exemple d'exécution

Python2.7


>>> import six
>>> six.PY2
True
>>> six.PY3
False
>>> six.PY34
False

Python3.6


>>> import six
>>> six.PY2
False
>>> six.PY3
True
>>> six.PY34
True

L'implémentation interne utilise le sys.version_info ci-dessus.

six.py


# Useful for very coarse version differentiation.
PY2 = sys.version_info[0] == 2
PY3 = sys.version_info[0] == 3
PY34 = sys.version_info[0:2] >= (3, 4)

Bonus: détermination de la version Python sur Shell Script

Une doublure pour obtenir la version majeure de Python

Python2/3


$ pymajorver=$(python -c "from __future__ import print_function; import sys; print(sys.version_info[0])")
$ echo $pymajorver
3

Vous pouvez également l'utiliser dans l'instruction case comme suit:

python


case $(python -c "from __future__ import print_function; import sys; print(sys.version_info[0])") in
    "2") echo "Python2 !!" ;;
    "3") echo "Python3 !!" ;;
    *) echo "Unknown Python version" ;;
esac

Une doublure pour obtenir la version majeure / mineure de Python

Prend en charge à la fois Python 2/3

Python2.7


$ python -c "from __future__ import print_function; import sys; print('{}{}'.format(*sys.version_info[0:2]))"
27

Python 3 uniquement

Python3.6


$ pyversion=$(python -c "import sys; print('{}{}'.format(*sys.version_info[0:2]))")
$ echo $pyversion
36

Recommended Posts

Comment obtenir la version Python
Comment changer la version de Python
Comment obtenir les fichiers dans le dossier [Python]
Comment obtenir le nom de la variable lui-même en python
Comment obtenir le nombre de chiffres en Python
Comment démarrer avec Python
Version Python pour obtenir les ports inutilisés
Comment obtenir la dernière (dernière) valeur d'une liste en Python
Comment entrer dans l'environnement de développement Python avec Vagrant
[Introduction à Python] Comment obtenir des données avec la fonction listdir
Comment vérifier la version de Django
Comment obtenir stacktrace en python
Comment vérifier la version d'opencv avec python
Comment obtenir une sortie colorée sur la console
Comment installer Python
Obtenez la version GNOME
Comment installer python
python / pandas / dataframe / Comment obtenir la ligne / colonne / index / colonne la plus simple
Comment mettre à jour la version Python de Cloud Shell dans GCP
Comment utiliser la bibliothèque C en Python
[Version 2020] Comment installer Python3 sur EC2 d'AWS
[Python] Comment changer le format de la date (format d'affichage)
Comprenons comment passer des arguments (édition Python)
Comment spécifier la version TLS dans les requêtes python
[Algorithm x Python] Comment utiliser la liste
Comment effacer les caractères générés par Python
Comment obtenir des éléments de type dictionnaire de Python 2.7
[Python Kivy] Comment obtenir le chemin du fichier par glisser-déposer
[Yahoo! Weather Replacement Version] Comment obtenir des informations météo avec LINE Notify + Python
[2020.8 dernière] Comment installer Python
Comment installer Python [Windows]
python3: Comment utiliser la bouteille (2)
Comment utiliser le générateur
[Python] Comment utiliser la liste 1
Comment mettre à jour Tkinter de Python vers la version 8.6
Comment utiliser Python Argparse
[Python] Récupérez le mois précédent
Python: comment utiliser pydub
[Python] Comment utiliser checkio
Comment exécuter Notepad ++ Python
Introduction à Python (version Python APG4b)
Comment développer en Python
[python] Comment juger scalaire
[Python] Comment utiliser input ()
Comment utiliser le décorateur
Comment utiliser Python lambda
[Python] Comment utiliser virtualenv
python3: Comment utiliser la bouteille (3)
python3: Comment utiliser la bouteille
Comment augmenter l'axe
Comment démarrer la première projection
Comment utiliser les octets Python
Comment récupérer la nième plus grande valeur en Python
Réfléchissez à la programmation de Python sur votre iPad
[Introduction à Python] Comment itérer avec la fonction range?
Comment connaître le répertoire actuel en Python dans Blender
Comment soumettre automatiquement des formulaires Microsoft à l'aide de python (version Mac)
[Réintroduction à python] Comment importer via le répertoire parent
Comment utiliser Raspeye Relay Module Python
[Python] Comment spécifier l'emplacement de téléchargement avec youtube-dl