I was about to ask, what is the goal of this? Is this to prevent users from removing version guard and force-running the code with older version anyway?
If it's a single file script, checking sys.version only works if all your code is syntactically correct for that version. It's a shortcoming of python's approach of parsing the whole file before handing control over to your code. Basically, the interpreter bombs before it ever runs your "sys.version" code.
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.
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.