snippetpythonCritical
How do I check which version of Python is running my script?
Viewed 0 times
howpythonscriptcheckrunningversionwhich
Problem
How do I check which version of the Python interpreter is running my script?
See Find full path of the Python interpreter (Python executable)? if you are looking to find exactly which interpreter is being used - for example, to debug a Pip installation problem, or to check which virtual environment (if any) is active.
See Find full path of the Python interpreter (Python executable)? if you are looking to find exactly which interpreter is being used - for example, to debug a Pip installation problem, or to check which virtual environment (if any) is active.
Solution
This information is available in the
Human readable:
For further processing, use
To ensure a script runs with a minimal version requirement of the Python interpreter add this to your code:
This compares major and minor version information. Add micro (=
sys.version string in the sys module:>>> import sysHuman readable:
>>> 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, use
sys.version_info or sys.hexversion:>>> sys.version_info
(2, 5, 2, 'final', 0)
# or
>>> sys.hexversion
34014192To ensure a script runs with a minimal version requirement of the Python interpreter add this to your code:
assert sys.version_info >= (2, 5)This compares major and minor version information. Add micro (=
0, 1, etc) and even releaselevel (='alpha','final', etc) to the tuple as you like. Note however, that it is almost always better to "duck" check if a certain feature is there, and if not, workaround (or bail out). Sometimes features go away in newer releases, being replaced by others.Code Snippets
>>> 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)]>>> sys.version_info
(2, 5, 2, 'final', 0)
# or
>>> sys.hexversion
34014192assert sys.version_info >= (2, 5)Context
Stack Overflow Q#1093322, score: 1717
Revisions (0)
No revisions yet.