Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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 sys.version Attribute
The sys module provides access to variables and functions that interact with the Python interpreter. The version attribute contains detailed version information ?
import sys print(sys.version)
3.10.7 (tags/v3.10.7:6cc6b13, Sep 5 2022, 14:08:36) [MSC v.1933 64 bit (AMD64)]
The version attribute returns a string containing the version number, build information, and compiler details.
Using the sys.version_info Attribute
The version_info attribute provides structured version information as a named tuple ?
import sys print(sys.version_info)
sys.version_info(major=3, minor=10, micro=7, releaselevel='final', serial=0)
Accessing Individual Components
You can access specific version components by name ?
import sys
info = sys.version_info
print(f"Major: {info.major}")
print(f"Minor: {info.minor}")
print(f"Micro: {info.micro}")
Major: 3 Minor: 10 Micro: 7
Using platform.python_version()
The platform module provides a clean method to get just the version string ?
import platform print(platform.python_version())
3.10.7
This method returns a string in 'major.minor.patchlevel' format, which is cleaner than sys.version.
Using platform.python_version_tuple()
For structured access to version components as strings, use python_version_tuple() ?
import platform
version_tuple = platform.python_version_tuple()
print(version_tuple)
print(f"Type: {type(version_tuple)}")
('3', '10', '7')
<class 'tuple'>
Comparison of Methods
| Method | Return Type | Format | Best For |
|---|---|---|---|
sys.version |
String | Detailed with build info | Complete version details |
sys.version_info |
Named tuple | Structured components | Version comparison logic |
platform.python_version() |
String | Clean 'X.Y.Z' format | Simple version display |
platform.python_version_tuple() |
Tuple | String components | String-based processing |
Conclusion
Use sys.version_info for version comparison logic, platform.python_version() for clean version display, and sys.version when you need complete build information.
