How can I check what version of the Python Interpreter is interpreting my script?
Here's a short commandline version which exits straight away (handy for scripts and automated execution):
python -c "print(__import__('sys').version)"
Or just the major, minor and micro:
python -c "print(__import__('sys').version_info[:1])" # (2,)
python -c "print(__import__('sys').version_info[:2])" # (2, 7)
python -c "print(__import__('sys').version_info[:3])" # (2, 7, 6)
Just type python in your terminal and you can see the version as like following
desktop:~$ python
Python 2.7.6 (default, Jun 22 2015, 18:00:18)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
A attempt using os.popen
to read it in a variable:
import os
ver = os.popen('python -V').read().strip()
print(ver)
Your best bet is probably something like so:
>>> import sys
>>> sys.version_info
(2, 6, 4, 'final', 0)
>>> if not sys.version_info[:2] == (2, 6):
... print "Error, I need python 2.6"
... else:
... from my_module import twoPointSixCode
>>>
Additionally, you can always wrap your imports in a simple try, which should catch syntax errors. And, to @Heikki's point, this code will be compatible with much older versions of python:
>>> try:
... from my_module import twoPointSixCode
... except Exception:
... print "can't import, probably because your python is too old!"
>>>
...you can (ab)use list comprehension scoping changes and do it in a single expression:
is_python_3_or_above = (lambda x: [x for x in [False]] and None or x)(True)
Check Python version: python -V
or python --version
or apt-cache policy python
you can also run whereis python
to see how many versions are installed.