3

As the question asks, I want to be sure that the script is executed by a specific version of python, say =>3.5.2.

How can I make sure that the script when executed is called by the specific version.

This check should be done in the python script itself.

It could be better if the solution is platform independent.

penta
  • 2,536
  • 4
  • 25
  • 50
  • Check this one: https://stackoverflow.com/questions/1093322/how-do-i-check-what-version-of-python-is-running-my-script – ayorgo Apr 28 '19 at 10:33
  • @Sanyash I wonder how one can reflag as duplicate if missed the first time. – ayorgo Apr 28 '19 at 10:35
  • @ayorgo Sorry, I don't know, maybe you should ask it on meta. Btw, from my experience, your wrong dupe flag will be marked as helpful it the question will be closed. So, you can just edit your comment as you did. – sanyassh Apr 28 '19 at 10:38
  • Honestly Sanyash was able to better understand my question & that is a correct link to the question. – penta Apr 28 '19 at 10:41
  • Possible duplicate of [How to make the Shebang be able to choose the correct Python interpreter between python3 and python3.5](https://stackoverflow.com/q/47882916/608639). Also see [Any reason not to use /usr/bin/env python2/python3 explicitly?](https://stackoverflow.com/q/45046409/608639) – jww Apr 28 '19 at 13:55

3 Answers3

5

Just add this to the start of your script:

import sys

assert sys.version_info >= (3, 5, 2), "Python version too low."

Or if you prefer without assert:

import sys

if not sys.version_info >= (3, 5, 2):
    raise EnvironmentError("Python version too low.")

Or little bit more verbosely, but this will actually inform the user what version they need:

import sys

MIN = (3, 5, 2)
if not sys.version_info >= MIN:
    raise EnvironmentError(
        "Python version too low, required at least {}".format('.'.join(str(n) for n in MIN)))

Example output:

Traceback (most recent call last):
  File "main.py", line 6, in <module>
    "Python version too low, required at least {}".format('.'.join(str(n) for n in MIN)))
OSError: Python version too low, required at least 3.5.2

 

The specified minimum version tuple can be as precise as you want. These are all valid:

MIN = (3,)
MIN = (3, 5)
MIN = (3, 5, 2)
ruohola
  • 21,987
  • 6
  • 62
  • 97
  • @brunns Yeah, very true, edited an other way of doing it. – ruohola Apr 28 '19 at 10:40
  • Not a perfect test. `assert sys.version_info >= (3, 8)` yields `SyntaxError: invalid syntax` on older python because I'm using the walrus operator (much later in the code). – Piotr Siupa Sep 01 '20 at 18:13
  • @NO_NAME You cannot avoid that, the syntax will always be parsed and validated before anything else. – ruohola Sep 01 '20 at 18:49
1

You can check the version by calling sys.version_info. This will give you a tuple in the form sys.version_info(major=3, minor=6, micro=5, releaselevel='final', serial=0).

Ollie
  • 1,641
  • 1
  • 13
  • 31
  • I want to be sure that the called python to be greater than the version supplied, not check if python 3.6.5 is installed, would that check be done by this ? – penta Apr 28 '19 at 10:31
  • You can compare the output of `sys.version_info` with what you want. `sys.version_info` will give you information about what version of python is currently being run. – Ollie Apr 28 '19 at 10:32
  • e.g. `sys.version_info.major >= 3 and sys.version_info.minor >= 5 and sys.version_info.micro >= 2` (although this isn't exactly the boolean expression you want, you still have to add checks for e.g. 3.6.1) – Ollie Apr 28 '19 at 10:33
  • 1
    actually @ruohola's answer has a much better way of comparing them – Ollie Apr 28 '19 at 10:38
0

Try:

if not sys.version_info >= (3, 5, 2):
    raise EnvironmentError()

Or, better still, some custom exception with a useful message,

brunns
  • 2,689
  • 1
  • 13
  • 24