Identifying the Running Python Version in Your Script
The Python interpreter version used to execute a script can be easily determined. Access the necessary information through the sys module.
In the sys module, the sys.version string provides a human-readable representation of the version:
>>> import sys >>> print(sys.version) # parentheses necessary in Python 3 2.5.2 (r252:60911, Jul 31 2008, 17:28:52) [GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)]
For further processing, utilize sys.version_info or sys.hexversion:
>>> sys.version_info (2, 5, 2, 'final', 0) >>> sys.hexversion 34014192
To ensure a script's execution with a minimum required Python version, add this code:
assert sys.version_info >= (2, 5)
This checks the major and minor versions. You can also include micro and releaselevel information as needed. However, it's typically better to perform a "duck" check to verify the presence of specific features and work around their absence as necessary.
The above is the detailed content of How Can I Determine and Check the Running Python Version in My Script?. For more information, please follow other related articles on the PHP Chinese website!