4

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.

5

3 Answers 3

6

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 sys.version_info < (3, 5, 2):
    raise EnvironmentError("Python version too low.")

Or little bit more verbosely, but this will actually inform what version is needed:

import sys

MIN_VERSION = (3, 5, 2)

if sys.version_info < MIN_VERSION:
    raise EnvironmentError(
        "Python version too low, required at least {}".format(
            '.'.join(str(n) for n in MIN_VERSION)
        )
    )

Example output:

Traceback (most recent call last):
  File "main.py", line 6, in <module>
    raise EnvironmentError(
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)
Sign up to request clarification or add additional context in comments.

5 Comments

@brunns Yeah, very true, edited an other way of doing it.
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).
@PiotrSiupa You could put this assertion as the first lines of your main.py before you import any code that uses newer Python syntax.
Not imports. Just some code in my script. Even if this test is a first thing in the script and the line using a new syntax is on the very bottom, the test still won't run because of a parsing error.
@PiotrSiupa Yes, that cannot be avoided if you have everything in the same file. But you could circumvent that problem by having your code that uses new Python syntax in separate file(s) that you import only after this check has been done.
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).

4 Comments

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 ?
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.
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)
actually @ruohola's answer has a much better way of comparing them
0

Try:

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

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.