14

I have a Python script that uses Python version 2.6 syntax (Except error as value:) which version 2.5 complains about. So in my script I have included some code to check for the Python interpreter version before proceeding so that the user doesn't get hit with a nasty error, however, no matter where I place that code, it doesn't work. Once it hits the strange syntax it throws the syntax error, disregarding any attempts of mine of version checking.

I know I could simply place a try/except block over the area that the SyntaxError occurs and generate the message there but I am wondering if there is a more "elegant" way. As I am not very keen on placing try/except blocks all over my code to address the version issue. I looked into using an __ init__.py file, but the user won't be importing/using my code as a package, so I don't think that route will work, unless I am missing something...

Here is my version checking code:

import sys
def isPythonVersion(version):
    if float(sys.version[:3]) >= version:
        return True
    else:
        return False

if not isPythonVersion(2.6):
    print "You are running Python version", sys.version[:3], ", version 2.6 or 2.7 is required. Please update. Aborting..."
    exit()

4 Answers 4

16

Something like this in beginning of code?

import sys
if sys.version_info<(2,6):
    raise SystemExit('Sorry, this code need Python 2.6 or higher')
Sign up to request clarification or add additional context in comments.

2 Comments

Use something with this, it's what sys.version_info exists for.
I had a similar problem and also had tried this approach, but, unfortunately, this trick does not work in cases where the code uses syntax that is not compatible with the version of python running. This is because the interpreter checks the syntax of your code before compiling/running it. So, the "if ..." statement will never be run if, say, you have some statement elsewhere in your code that isn't compatible with the current version of python (e.g., print(..., file=sys.stderr), where the 'file' keyword is not compatible with older python.
15

Create a wrapper script that checks the version and calls your real script -- this gives you a chance to check the version before the interpreter tries to syntax-check the real script.

1 Comment

How is it unnecessary? The entire module is syntax checked before a single line of code is run, so it's not possible to catch SyntaxError or perform version checks before it's too late without a wrapper script.
9

In sys.version_info you will find the version information stored in a tuple:

sys.version_info
(2, 6, 6, 'final', 0)

Now you can compare:

def isPythonVersion(version):
    return version >= sys.version_info[0] + sys.version_info[1] / 10.

2 Comments

Thanks but I already have a method of checking this, shown above.
This would not be reliable, as the division behaves differently between 2 and 3, For example 2.6 would not be equal to 2 + (6/10). 2 + (6/10)= 2.0
1

If speed is not a priority, you can avoid this problem entirely by using sys.exc_info to grab the details of the last exception.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.