- Trending Categories
- Data Structure
- Operating System
- MS Excel
- C Programming
- Social Studies
- Fashion Studies
- Legal Studies
- 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 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.
The version attribute returns a string containing the version number of the Python interpreter with additional information on the build number and used compiler.
>>> import sys >>> sys.version
'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
The version_info attribute returns a tuple that provides major, minor and micro version levels.
>>> import sys >>> sys.version_info
sys.version_info(major=3, minor=10, micro=7, releaselevel='final', serial=0)
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)
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
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()
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.
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.
The following example can be used on Windows, Mac, Linux, and Ubuntu operating systems.
('3', '10', '7') <class 'tuple'>
Kickstart Your Career
Get certified by completing the courseGet Started