python 2 code: if python 3 then sys.exit()

Python will byte-compile your source file before starting to execute it. The whole file must at least parse correctly, otherwise you will get a SyntaxError.

The easiest solution for your problem is to write a small wrapper that parses as both, Python 2.x and 3.x. Example:

import sys
if sys.version_info >= (3, 0):
    sys.stdout.write("Sorry, requires Python 2.x, not Python 3.x\n")
    sys.exit(1)

import the_real_thing
if __name__ == "__main__":
    the_real_thing.main()

The statement import the_real_thing will only be executed after the if statement, so the code in this module is not required to parse as Python 3.x code.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)