sorin sorin - 2 years ago 66
Python Question

How to write Python code that is able to properly require a minimal python version?

I would like to see if there is any way of requiring a minimal python version.

I have several python modules that are requiring Python 2.6 due to the new exception handling (


It looks that even if I check the python version at the beginning of my script, the code will not run because the interpreter will fail inside the module, throwing an ugly system error instead of telling the user to use a newer python.

Answer Source
import sys
if sys.hexversion < 0x02060000:
    sys.exit("Python 2.6 or newer is required to run this program.")

import module_requiring_26

Also the cool part about this is that it can be included inside the __init__ file or the module.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download