I'm trying to write a block of code at the top of my programs so that, if the program is accidentally run in Python 2.x, it will give an error message and quit; but if run in Python 3.x will run normally. The idea is to use a print statement that is valid in 2.x but not in 3.x:
try:
print "Error: This program should only be run in Python 3."
raw_input('>')
exit()
except SyntaxError:
pass
print("I see you're running Python 3.")
# rest of program
But when I try the code in Python 3, the expected SyntaxError is not caught and the program does not proceed:
File "version.py", line 2
print "This program should only be run in Python 3"
^
SyntaxError: invalid syntax
Why not?