Check Python version from inside script? Run Pythons script in v2 compatibility mode?

Marko Rauhamaa marko at pacujo.net
Fri Jul 7 07:16:09 EDT 2017


Pavol Lisy <pavol.lisy at gmail.com>:

> On 7/7/17, Steve D'Aprano <steve+python at pearwood.info> wrote:
>
>> import sys
>> if sys.version_info >= (3,):  # the comma is important
>>     print("version 3")
>
> But be careful inside script! It could live long enough to see python4
> :)

That's a serious concern. An application doesn't know about Python's
future. What would be needed is:

   import sys
   try:
       sys.require_version((3, 4, 2))
   except NotSupportedException:
       sys.stderr.write("Sorry :(\n")
       sys.exit(1)


Marko



More information about the Python-list mailing list