Just check values in sys.version_info then throw an exception, it's so much more obvious what you're doing.
Just check values in sys.version_info then throw an exception, it's so much more obvious what you're doing.
If your __init__.py didn’t have any version-specific syntax, then yes you could check the interpreter version before importing the rest of the files.
Besides, __init__.py doesn't always exist.
# -*- python >= 3.1 -*-
The trouble is, that wouldn't work on the old versions!Yeah, a bit late to address the issue. Would have been helpful in the 2->3 transition.
Partially because people avoid Python for things like installers and simple sharable single file scripts...because of this. There are ways around it in many other scripting languages. And a fairly good number of popular "single file scripts" that get shared, like mysqltuner (perl), adminer (php), etc.
Instead of checking whether proper version is used, why not just ensure it runs on right now (and perhaps includes needed dependencies)
It's actually a fairly practical approach, where you are controlling where the code is going to bomb out, and including a helpful comment that will show in the stack trace / exception.
With Perl, for example, you can have a BEGIN block to check for versions, and it works as expected, even if there's code in the main body that's too new for whatever version of Perl is running. Python doesn't have that ability unless your code is split across more than one file and using conditional imports. Some BEGIN block type functionality for python seems like it would have been useful to me.