How do I check what version of Python is running my script?


Python is being updated regularly with new features and supports. Starting from 1994 to current release, there are lots of updates in Python versions.

By using Python standard libraries like sys or platform modules we can get the version information of Python that is actually running on our script.

In general the python version is displayed automatically on the console immediately after starting the interpreter from the command line.

Python 3.10.7 (tags/v3.10.7:6cc6b13, Sep  5 2022, 14:08:36) [MSC v.1933 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.

Using the version attribute

The sys module usually provides the access to some variables and functions that are used to maintained the interpreter. Here we can use this module to get the python version information in our script.

First of all, we need to import the sys module from the standard library, and the version information is present in the version attribute defined in the sys module.

sys.version

The version attribute returns a string containing the version number of the Python interpreter with additional information on the build number and used compiler.

Example

>>> import sys
>>>  sys.version

Output

'3.10.7 (tags/v3.10.7:6cc6b13, Sep  5 2022, 14:08:36) [MSC v.1933 64 bit (AMD64)]'

Using the version_info attribute

Also we can use another attribute version_info to get the python version, and it gives the information more elaborately. Following is the syntax

sys.version_info

Example

The version_info attribute returns a tuple that provides major, minor and micro version levels.

>>>  import sys
>>>  sys.version_info

Output

sys.version_info(major=3, minor=10, micro=7, releaselevel='final', serial=0)

Example

From the output of version_info attribute we can access the information by name such as major or minor.

info = sys.version_info
print(info.major)
print(info.minor)

Output

3
10

Using the python_version() method of platform module

The Platform module is a Python package that provides an API to retrieve all information regarding the platform/system where our code is running. Platform is an inbuilt module in python so that we can directly import it in our script. And the python version information is present in the python_version() method. Following is the syntax

platform.python_version()

Example

The python_version() method throws a string containing information about the python version in the form of 'major.minor.patchlevel'.

import platform
platform.python_version()

Output

'3.10.7'

Using the python_version_tuple() method

Another method called platform.python_version_tuple(), which is also used to get the python version in our running script.

Syntax

platform.python_version_tuple()

The python_version_tuple() in the platform module returns the Python version in the form of tuple, the first element represents the major, second element represents minor, and last element represents patchlevel.

Example

The following example can be used on Windows, Mac, Linux, and Ubuntu operating systems.

print(type(print(platform.python_version_tuple())))
print(type(platform.python_version_tuple()))

Output

('3', '10', '7')
<class 'tuple'>

Updated on: 24-Aug-2023

251 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements