
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How do I check what version of Python is running my script?
Python is being updated regularly with new features and support. Starting from 1994 to the current release, there have been lots of updates in Python versions.
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 access to some variables and functions that are used to maintain 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
Following is an example to find the version of Python using the sys module -
>>> import sys >>> sys.version
Following is an output of the above code -
'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
Following is an output of the above code -
sys.version_info(major=3, minor=10, micro=7, releaselevel='final', serial=0)
Example
From the output of the 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)
Following is an output of the above code ?
3 10
Using the platform.python_version() Method
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()
Following is an output of the above code ?
'3.10.7'
Using python_version_tuple() Method
Another method is called platform.python_version_tuple(), which is also used to get the Python version in our running script.
Syntax
Following is the syntax of the platform Python version tuple() method -
platform.python_version_tuple()
The python_version_tuple() in the platform module returns the Python version in the form of a tuple; the first element represents the major version, the second element represents the minor version, and the last element represents patch level.
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()))
Following is an output of the above code -
('3', '10', '7') <class 'tuple'>