1

I'm trying to write a block of code at the top of my programs 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:

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

This works correctly in Python 2.7 (i.e., it displays the error and quits), but when I run it in Python 3.3, I get a SyntaxError, even though I told it to have an exception.

  File "version.py", line 2
    print "This program should only be run in Python 3"
                                                      ^
SyntaxError: invalid syntax

What am I missing?

foltor
  • 61
  • 1
  • 4
  • What you want is to detect python – Loïc Faure-Lacroix Feb 09 '14 at 18:24
  • Syntax checking is phenomena of compilation time is that how to parse an expression (whether it is correct? How to recover from errors) But exceptions that you catch are runtime. An syntax error can't be catch because it structure the code in a meaningful way. – Grijesh Chauhan Feb 09 '14 at 18:33

2 Answers2

7

SyntaxErrors are thrown at compile-time. You cannot catch them like runtime exceptions.

If you want to check python version, look at sys.version_info.

i.e.

import sys
if sys.version_info.major < 3:
    sys.exit('This program should only be run in Python 3')
roippi
  • 25,533
  • 4
  • 48
  • 73
  • Some Syntax Errors (`ast.literal_eval('1a')`) can be caught at runtime. – Ashwini Chaudhary Feb 09 '14 at 18:36
  • Yes, runtime `SyntaxErrors` can be caught at runtime - the point is your script needs to compile before it can throw any runtime errors :) – roippi Feb 09 '14 at 18:49
  • @roippi One man's compile time is another man's run time. Since Python programs are typically parsed and compiled just before being run, and almost always in response to other Python programs (e.g. via `import`), a distinction between compile and run time is artificial and mostly useless. What matters is whether the relationship between modules, who observes the compilation of whom. –  Feb 09 '14 at 18:52
  • @delnan I'm aware. The statement "you cannot catch them *like* runtime exceptions" was deliberately phrased - they *can* be caught, just not in the place/manner you catch the majority of exceptions. For the purposes of this question that is largely tangential, the point is that the OP should be using `sys.version_info` to check version, not a side-effect of some parsing. – roippi Feb 09 '14 at 19:01
1

Here's a small example on how to do what you want:

import sys

if sys.version_info.major == 2:
    print("Error: This program should only be run in Python 3.")
    raw_input('>')
    exit(0)

This code will run in python2 and python3. You don't need to use a try catch in that case. Since in python2, it happens that the ( and ) will have no effect since they aren't tuples. You can write that kind of thing and it will work in python2 and python3 without SyntaxError.

Loïc Faure-Lacroix
  • 13,220
  • 6
  • 67
  • 99