This article explains how to check, obtain, and print the version of Python installed and running on Windows, Mac, and Linux.
Check the Python version on the command line: -version, –
- V, -VV Check
- Python version in the script: sys, platform
- Miscellaneous information string: sys.version
- number
- tuple
- : platform.python_version_tuple()
Version number tuple: sys.version_info Version number string: platform.python_version() Version
string
the
If you want to check the package/library version, operating system, etc., rather than the Python version itself, check out the following articles.
Check the version of the Python
- package/library
- Get the operating system and its version where Python runs
Check the Python
version on the
command line:
-version, -V, -VV Run the python or
python3 command with the -version or -V option at the command prompt (cmd) on Windows or the terminal on Mac and Linux.
As shown in the previous example, In some environments, the Python2.x string is mapped to the python command and the Python3.x string is mapped to the python3 command.
The -VV option was introduced in Python 3.6 and provides more detailed information than -V.
Check the Python version in the script: sys, platform
You can use the standard library sys module or the platform module to get the
version of Python that is actually running.
The same script works on Windows, Mac, and Linux, including Ubuntu
. It is useful to check which version of Python is running when multiple Python installations exist
in the
same environment. Sometimes, you can assume that Python3 is running, but Python2 is actually running instead. If you encounter any problems, it’s a good idea to check the running version.
It can also be used when changing operations depending on whether it is Python2 or Python3.
Miscellaneous
information string: sys.version
sys.version is a string that contains miscellaneous information, including the version number.
sys.version A string that contains the version number of the Python interpreter plus additional information about the build number and compiler used. sys.version — System-specific parameters and functions — Python 3.11.2 documentation
Version number tuple:
sys.version_info sys.version_info is a tuple (called a tuple) that indicates the
version number.
sys.version_info A tuple containing the five components of the version number: major, minor, micro, release level, and serial. sys.version_info — System-specific parameters and functions — Python 3.11.2 documentation
releaselevel is str, and the other elements are int
.
You can get each value by specifying an index
.
Starting with version 2.7 for Python2 and version 3.1 for Python3, you can get items by name (major, minor, micro, release level, serial).
For example, if you want to get a major version:
To determine whether Python2 or Python3 is running, check the major version with this sys.version_info.major. 2 means Python2 and 3 means Python3. You can switch the process between Python2 or Python3.
Use sys.version_info.minor if you want to change operations based on a minor version.
As mentioned earlier, accessing elements using names starting with version
2.7 for Python2 and version 3.1 for Python3
is supported.
If your code can run in earlier versions, use sys.version_info[0] or sys.version_info[1] instead
.
Version number
string: platform.python_version() platform.python_version() returns a string ‘major.minor.patchlevel’
.
platform.python_version() Returns the Python version as string ‘
major.minor.patchlevel
‘. platform.python_version — Accessing Underlying Platform Identification Data — Python 3.11.2 documentation
It is useful when you want to get the version number as a simple string.
Version number string tuple: platform.python_version_tuple() platform.python_version_tuple() returns a
tuple (
larger, smaller, patch level). The type of each element is str, not int.
platform.python_version_tuple() Returns the Python version as a tuple (major, minor,
patch level) of strings. platform.python_version_tuple — Access to Underlying Platform Identification Data — Python 3.11.2 documentation
Since it is just a tuple, unlike sys.version_info, it cannot be accessed by name such as major or minor.