How to get the Python version

Shows how to get the Python version from a running Python program and an execution example. There are multiple ways to get the version:

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

Method 1: sys.version_info

sys.version_info stores the Python version of the execution environment. For Python less than 2.7 / 3.0, the type is tuple, and for 2.7 / 3.1 and later, the type is named tuple.

Execution example

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

Method 2: platform.python_version_tuple ()

platform.python_version_tuple () gets the version number information as tuple regardless of the Python version.

Execution example

>>> 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').

Method 3: six

six can be used to determine whether Python 2 or Python 3 (and Python 3.4 or higher can be determined).

Execution example

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

The internal implementation uses the above 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)

Bonus: Python version determination on Shell Script

One liner to get the major version of Python

Python2/3


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

You can also use it in the case statement as follows:

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

One liner to get major / minor version of Python

Supports both Python 2/3

Python2.7


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

Python 3 only

Python3.6


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

Recommended Posts

How to get the Python version
How to change Python version
How to get the files in the [Python] folder
How to get the variable name itself in python
How to get the number of digits in Python
How to get started with Python
[Python] How to import the library
Python version to get unused ports
How to get the last (last) value in a list in Python
How to get into the python development environment with Vagrant
[Introduction to Python] How to get data with the listdir function
How to check the version of Django
How to get a stacktrace in python
How to check opencv version in python
How to get colored output to the console
How to install Python
Get the GNOME version
How to install python
python / pandas / dataframe / How to get the simplest row / column / index / column
How to update the python version of Cloud Shell on GCP
How to use the C library in Python
[2020 version] How to install Python3 on AWS EC2
[Python] How to change the date format (display format)
Let's understand how to pass arguments (Python version)
How to specify TLS version in python requests
[Algorithm x Python] How to use the list
How to erase the characters output by Python
How to get dictionary type elements of Python 2.7
[Python Kivy] How to get the file path by dragging and dropping
[Yahoo! Weather Replacement Version] How to get weather information with LINE Notify + Python
[2020.8 latest] How to install Python
How to install Python [Windows]
python3: How to use bottle (2)
How to use the generator
[Python] How to use list 1
How to update Python Tkinter to 8.6
How to use Python argparse
[Python] Get the previous month
Python: How to use pydub
[Python] How to use checkio
How to run Notepad ++ Python
Introduction to Python (Python version APG4b)
How to develop in Python
[python] How to judge scalar
[Python] How to use input ()
How to use the decorator
How to use Python lambda
[Python] How to use virtualenv
python3: How to use bottle (3)
python3: How to use bottle
How to increase the axis
How to start the program
How to use Python bytes
How to retrieve the nth largest value in Python
Think about how to program Python on the iPad
[Introduction to Python] How to iterate with the range function?
How to know the current directory in Python in Blender
How to auto-submit Microsoft Forms using python (Mac version)
[Reintroduction to python] How to import via the parent directory
How to use the Raspberry Pi relay module Python
[Python] How to specify the download location with youtube-dl