1

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?

2
  • What you want is to detect python Commented Feb 9, 2014 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. Commented Feb 9, 2014 at 18:33

2 Answers 2

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')
Sign up to request clarification or add additional context in comments.

4 Comments

Some Syntax Errors (ast.literal_eval('1a')) can be caught at runtime.
Yes, runtime SyntaxErrors can be caught at runtime - the point is your script needs to compile before it can throw any runtime errors :)
@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.
@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.
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.

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.