Hier erfahren Sie, wie Sie die Python-Version aus einem laufenden Python-Programm und einem Beispiel abrufen. Es gibt mehrere Möglichkeiten, um die Version zu erhalten:
sys.version_info
platform.python_version_tuple()
six
sys.version_info
speichert die Python-Version der Ausführungsumgebung.
Für Python unter 2.7 / 3.0 lautet der Typ "Tupel", und für 2.7 / 3.1 und höher heißt der Typ "Tupel".
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
platform.python_version_tuple ()
ruft die Versionsnummerninformationen unabhängig von der Python-Version als tuple
ab.
>>> 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').
six kann verwendet werden, um festzustellen, ob es sich um Python 2 oder Python 3 (und Python 3.4 oder höher) handelt.
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
Die interne Implementierung verwendet die obige sys.version_info
.
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)
Python2/3
$ pymajorver=$(python -c "from __future__ import print_function; import sys; print(sys.version_info[0])")
$ echo $pymajorver
3
Sie können es auch in der "case" -Anweisung wie folgt verwenden:
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
Python2.7
$ python -c "from __future__ import print_function; import sys; print('{}{}'.format(*sys.version_info[0:2]))"
27
Python3.6
$ pyversion=$(python -c "import sys; print('{}{}'.format(*sys.version_info[0:2]))")
$ echo $pyversion
36
Recommended Posts