Check Python Version on Command Line and in Scripts

Modified: | Tags: Python

This article explains how to check, get, and print the installed Python version and the one currently used to run scripts on Windows, macOS, and Linux.

For related topics, such as checking the version of installed packages or your operating system, see the following articles.

Check the Python version on the command line: --version, -V, -VV

Run the python or python3 command with the --version or -V option in the Command Prompt (cmd) on Windows or the Terminal on macOS and Linux.

$ python3 --version
Python 3.11.3

$ python3 -V
Python 3.11.3

In some environments, the python command is assigned to the Python 2.x series, while the python3 command is assigned to the Python 3.x series.

The -VV option was introduced in Python 3.6 and provides more detailed information than -V.

$ python3 -VV
Python 3.11.3 (main, Apr  7 2023, 20:13:31) [Clang 14.0.0 (clang-1400.0.29.202)]

Check the Python version in scripts: sys, platform

You can get the currently running Python version using the standard library's sys or platform module. No additional installation is required, but you need to import them.

import sys
import platform

The same script works across Windows, macOS, and Linux distributions such as Ubuntu.

It is helpful to verify the running Python version, especially in environments with multiple installations. You might assume you are using Python 3, but Python 2 could be running instead. If you face any issues, it is a good idea to confirm the active version.

You can also change behavior based on whether Python 2 or Python 3 is running.

Version information as a string: sys.version

sys.version provides a string that shows various information including the version number.

sys.version
A string containing the version number of the Python interpreter plus additional information on the build number and compiler used.
sys.version — System-specific parameters and functions — Python 3.13.3 documentation

print(sys.version)
# 3.11.3 (main, Apr  7 2023, 20:13:31) [Clang 14.0.0 (clang-1400.0.29.202)]

print(type(sys.version))
# <class 'str'>

Version number as a tuple: sys.version_info

sys.version_info provides a named tuple indicating the version number.

sys.version_info
A tuple containing the five components of the version number: major, minor, micro, releaselevel, and serial.
sys.version_info — System-specific parameters and functions — Python 3.13.3 documentation

print(sys.version_info)
# sys.version_info(major=3, minor=11, micro=3, releaselevel='final', serial=0)

print(type(sys.version_info))
# <class 'sys.version_info'>

releaselevel is a string (str), while the other elements are integers (int).

You can get each value by specifying an index.

print(sys.version_info[0])
# 3

From Python 2.7 and Python 3.1 onwards, elements can be accessed by name, such as major, minor, micro, releaselevel, and serial.

For example, if you want to get a major version:

print(sys.version_info.major)
# 3

To determine whether Python 2 or Python 3 is running, check the major version with sys.version_info[0] or sys.version_info.major. 2 indicates Python 2, and 3 indicates Python 3.

You can switch the process between Python 2 and Python 3.

if sys.version_info[0] == 3:
    print('Python3')
else:
    print('Python2')
# Python3

To switch operations based on the minor version, use sys.version_info[1] or sys.version_info.minor.

As mentioned above, accessing elements by name is supported from version 2.7 and version 3.1. If the code might be executed in earlier versions, use the index like sys.version_info[0] for the major version or sys.version_info[1] for the minor version.

Version number as a 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 — Access to underlying platform’s identifying data — Python 3.13.3 documentation

print(platform.python_version())
# 3.11.3

print(type(platform.python_version()))
# <class 'str'>

This is useful when you need the version as a simple string.

Version number as a tuple of strings: platform.python_version_tuple()

platform.python_version_tuple() returns a tuple (major, minor, patchlevel). The type of each element is str, not int.

platform.python_version_tuple()
Returns the Python version as tuple (major, minor, patchlevel) of strings.
platform.python_version_tuple — Access to underlying platform’s identifying data — Python 3.13.3 documentation

print(platform.python_version_tuple())
# ('3', '11', '3')

print(type(platform.python_version_tuple()))
# <class 'tuple'>

Unlike sys.version_info, it is a regular tuple and does not support named attributes like major or minor.

Related Categories

Related Articles