This section shows how to get, check, and display the installed Python version and the version of Python that is actually running in the script.
This section explains how to check the command line and code, respectively.
- Check and display the version on the command line:
--version
,-V
,-VV
- Get the version in the code:
sys
,platform
- A string of various information, including a version number:
sys.version
- A numeric tuple of version numbers:
sys.version_info
- Version number string:
platform.python_version()
- A tuple of version number strings:
platform.python_version_tuple()
- A string of various information, including a version number:
If you get the version number in the code, you can display it with print() to check it, and also switch the process depending on the version.
Check and display the version on the command line: –version, -V, -VV
You can use a command prompt for Windows, or a terminal for Mac.python
command or thepython3
command.--version
Optional or-V
option to run it.
$ python --version
Python 2.7.15
$ python -V
Python 2.7.15
$ python3 --version
Python 3.7.0
$ python3 -V
Python 3.7.0
As you can see in the example above, depending on your environment, the Python 2.x system may bepython
command, the Python 3.x series will bepython3
It is assigned to a command.
From Python 3.6-VV
option has been added.-V
You can see more detailed information than
$ python3 -VV
Python 3.7.0 (default, Jun 29 2018, 20:13:13)
[Clang 9.1.0 (clang-902.0.39.2)]
Get version in code: sys, platform
You can also use the standard library's sys module or platform module to get, check, and display the version of Python that is actually running.
Run the Python script to check. The script is the same for Windows, Mac, Ubuntu, and other Linux systems.
This is useful for checking which version of Python is being used in an environment where multiple versions of Python are installed, as it is possible to run Python 2 when you thought you were running Python 3.
It can also be used for conditional branching when you want to switch between Python 2 and Python 3 processing.
Various strings of information, including version number: sys.version
sys.version
is a string that indicates various information, including the version number.
sys.version
A string indicating the Python interpreter version number as well as information such as the build number and compiler used.
sys — System-specific parameters and functions – Python 3.10.0 Documentation
import sys
print(sys.version)
# 3.7.0 (default, Jun 29 2018, 20:13:13)
# [Clang 9.1.0 (clang-902.0.39.2)]
print(type(sys.version))
# <class 'str'>
Numerical tuple of version number: sys.version_info
sys.version_info
is a tuple indicating the version number.
sys.version_info
A tuple of five values indicating the version number: major, minor, micro, releaselevel, and serial. All values except releaselevel are integers.sys — System-specific parameters and functions – Python 3.10.0 Documentation
print(sys.version_info)
# sys.version_info(major=3, minor=7, micro=0, releaselevel='final', serial=0)
print(type(sys.version_info))
# <class 'sys.version_info'>
releaselevel
is a string, and all other elements are integers.
You can specify the index to get the respective value.
print(sys.version_info[0])
# 3
Starting from version 2.7 for Python 2 series and from version 3.1 for Python 3 series, element access using names (seemajor
minor
micro
releaselevel
serial
For example, if you want to get the major version, you can use For example, if you want to get the major version, you can do the following
print(sys.version_info.major)
# 3
If you want to determine whether you are running Python2 or Python3, use thesys.version_info.major
You can check the major version in2
Then you can use Python2 to3
Then Python3.
An example of switching between Python 2 and Python 3 processing is shown below.
if sys.version_info.major == 3:
print('Python3')
else:
print('Python2')
# Python3
If you want to switch the process in a minor versionsys.version_info.minor
Determine the
Note that, as mentioned above, element access by name is supported from version 2.7 and 3.1, so if you are likely to run it in an earlier version, you can usesys.version_info[0]
and … andsys.version_info[1]
specified by index.
Version number string: platform.python_version()
platform.python_version()
is.major.minor.patchlevel
A function that returns a string in the format
platform.python_version()
Returns the Python version as a string in the format 'major.minor.patchlevel'.
platform — Access to underlying platform’s identifying data – Python 3.10.0 Documentation
import platform
print(platform.python_version())
# 3.7.0
print(type(platform.python_version()))
# <class 'str'>
Useful when you want to get the version number as a simple string.
Tuple of version number strings: platform.python_version_tuple()
platform.python_version_tuple()
is.(major, minor, patchlevel)
A function that returns a tuple of The content of the tuple is not a number but a string.
platform.python_version_tuple()
Returns the Python version as a tuple of strings (major, minor, patchlevel).
platform — Access to underlying platform’s identifying data – Python 3.10.0 Documentation
print(platform.python_version_tuple())
# ('3', '7', '0')
print(type(platform.python_version_tuple()))
# <class 'tuple'>
sys.version_info
Since it is just a tuple, unlikemajor
and … andminor
element access by name is not allowed.
Check and display the Python version (e.g. sys.version)
This section shows how to get, check, and display the installed Python version and the version of Python that is actually running in the script.
This section explains how to check the command line and code, respectively.
- Check and display the version on the command line:
--version
,-V
,-VV
- Get the version in the code: sys,platform
- A string of various information, including version number: sys.version
- A numeric tuple of version numbers: sys.version_info
- Version number string: platform.python_version()
- Tuple of version number strings: platform.python_version_tuple()
If you get the version number in the code, you can use the following function to display and check it.print()
You can also switch the process depending on the version.
Check and display the version on the command line: –version, -V, -VV
You can check the version by executing the following command from the command prompt on Windows or Terminal on Mac.
- command
python
python3
- Option
--version
-V
$ python --version
Python 2.7.15
$ python -V
Python 2.7.15
$ python3 --version
Python 3.7.0
$ python3 -V
Python 3.7.0
As shown in the example above, depending on the environment, Python 2.x systems are assigned to the python command and Python 3.x systems are assigned to the python3 command.
The -VV option was added in Python 3.6. The -VV option displays more detailed information than the -V option.
$ python3 -VV
Python 3.7.0 (default, Oct 21 2020, 10:23:15)
[Clang 9.1.0 (clang-902.0.39.2)]
Get version in code: sys, platform
You can also use the standard library's sys module or platform module to get, check, and display the version of Python that is actually running.
Run the Python script to check. The script is the same for Windows, Mac, Ubuntu, and other Linux systems.
This is useful for checking which version of Python is being used in an environment where multiple versions of Python are installed, as it is possible to run Python 2 when you thought you were running Python 3.
It can also be used for conditional branching when you want to switch between Python 2 and Python 3 processing.
Various strings of information, including version number: sys.version
sys.version
This is a string that indicates various information, including the version number.
sys.version
A string indicating the Python interpreter version number as well as information such as the build number and compiler used.
sys — System-specific parameters and functions – Python 3.10.0 Documentation
import sys
print(sys.version)
# 3.7.0 (default, Oct 21 2020, 10:23:15)
# [Clang 9.1.0 (clang-902.0.39.2)]
print(type(sys.version))
# <class 'str'>
Numerical tuple of version number: sys.version_info
sys.version_info
This is a tuple that indicates the version number.
sys.version_info
A tuple of five values indicating the version number: major, minor, micro, releaselevel, and serial, all of which are integers except releaselevel.
sys — System-specific parameters and functions – Python 3.10.0 Documentation
print(sys.version_info)
# sys.version_info(major=3, minor=7, micro=0, releaselevel='final', serial=0)
print(type(sys.version_info))
# <class 'sys.version_info'>
releaselevel
This is a string, and all other elements are integers.
You can specify the index to get the respective value.
print(sys.version_info[0])
# 3
As of version 2.7 for Python 2 series and version 3.1 for Python 3 series, the following element access by name is also supported.
major
minor
micro
releaselevel
serial
For example, if you want to get the major version, do the following
print(sys.version_info.major)
# 3
If you want to determine whether you are running Python 2 or Python 3, you can use the following code to check the major version.sys.version_info.major
If the return value is 2, it is Python2, if it is 3, it is Python3.
An example of switching between Python 2 and Python 3 processing is shown below.
if sys.version_info.major == 3:
print('Python3')
else:
print('Python2')
# Python3
If you want to switch the process with a minor version, determine the following values.sys.version_info.minor
Note that, as mentioned above, element access by name is supported from version 2.7 and 3.1, so if it may be executed in earlier versions, specify it by index as follows.
sys.version_info[0]
sys.version_info[1]
Version number string: platform.python_version()
platform.python_version() is a function that returns a string in major.minor.patchlevel format.
platform.python_version()
Returns the Python version as a string in the format 'major.minor.patchlevel'.
platform — Access to underlying platform’s identifying data – Python 3.10.0 Documentation
import platform
print(platform.python_version())
# 3.7.0
print(type(platform.python_version()))
# <class 'str'>
Useful when you want to get the version number as a simple string.
Tuple of version number strings: platform.python_version_tuple()
platform.python_version_tuple() is a function that returns a tuple of (major, minor, patchlevel).
The contents of a tuple is not a number, but a string.
platform.python_version_tuple()
Returns the Python version as a tuple of strings (major, minor, patchlevel).
platform — Access to underlying platform’s identifying data – Python 3.10.0 Documentation
print(platform.python_version_tuple())
# ('3', '7', '0')
print(type(platform.python_version_tuple()))
# <class 'tuple'>
Unlike sys.version_info, it is just a tuple, so element access by name is not possible.