Today we’ll see how to programmatically check a couple of things. In particular, we’ll be checking the version of the Python interpreter you’re using, the absolute path to the executable binary of the Python interpreter and the platform on which Python is running.
We’ll need the sys module, so let’s import it:
import sys
Now we can use the version static object to retrieve the version of the interpreter as a string:
>>> sys.version
'3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC v.1914 64 bit (AMD64)]'
We can also use the version_info static object to get version information as a named tuple.
>>> sys.version_info
sys.version_info(major=3, minor=7, micro=0, releaselevel='final', serial=0)
If we want, we can also check any particular element of the version, like the major version or the minor version for example:
>>> sys.version_info.major
3
We use the executable static object to check the absolute path of the executable binary of the Python interpreter:
>>> sys.executable
'C:\\Users\\user-1\\Source\\Repos\\Code\\Code\\env\\Scripts\\python.exe'
And here’s how we can check the platform on which Python is running:
>>> sys.platform
'win32'
Here’s the video version of the article: