[Python-ideas] Python Pragmas
db3l.net at gmail.com
Sat Mar 15 19:59:07 CET 2008
Larry Hastings <larry at hastings.org> writes:
> Recently-ish on c.l.py3k (iirc) folks were discussing how to write a
> script that exited with a human-friendly warning message if run under an
> incompatible version of the language. The problem with this code:
> import sys
> if sys.version < 3: sys.exit("Sorry, this script needs Python 3000")
> is that the code only executes once tokenization is finished--if your
> script uses any incompatible syntax, it will fail in the tokenizer, most
> likely with an error message that doesn't make it particularly clear
> what is going on.
Personally, in scenarios where I'm worried about that, I just make my
entry point script a thin one with code suitable for any releases I'm
worried about, and only import the main script once the version checks
have passed. It also permits conditional importing of a version-specific
script when that's appropriate as well.
Avoids the need to introduce execution into the tokenizer, and trying
to worry about all the possible types of comparisons you might want to
make (and thus need to be supported by such execution).
More information about the Python-ideas