I would like to check if my interpreter is using Python 3, before parsing the entire file, to print a decent error message otherwise.
In this question, there is an answer regarding Python 2.4 / 2.5. However, when I tried to reproduce the solution in my Python 2.7 / 3.3 environment, it did not work.
For instance, running the following file:
try:
eval("print('', end='')")
except SyntaxError:
raise Exception("python 3 needed")
print("", end="")
Results in the following error:
File "test_version.py", line 6
print("", end="")
^
SyntaxError: invalid syntax
That is, eval
is not evaluated soon enough to prevent the parser from reaching line 6.
Is there another hack that might work here?