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

后端 未结 9 763
面向向阳花
面向向阳花 2021-01-31 01:33

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 handl

相关标签:
9条回答
  • 2021-01-31 02:14

    Starting with version 9.0.0 pip supports Requires-Python field in distribution's metadata which can be written by setuptools starting with version 24-2-0. This feature is available through python_requires keyword argument to setup function.

    Example (in setup.py):

    setup(
    ...
       python_requires='>=2.5,<2.7',
    ...
    
    )
    

    To take advantage of this feature one has to package the project/script first if not already done. This is very easy in typical case and should be done nonetheless as it allows users to easily install, use and uninstall given project/script. Please see Python Packaging User Guide for details.

    0 讨论(0)
  • 2021-01-31 02:14
    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.

    0 讨论(0)
  • 2021-01-31 02:16

    Rather than indexing you could always do this,

    import platform
    if platform.python_version() not in ('2.6.6'):
        raise RuntimeError('Not right version')
    
    0 讨论(0)
提交回复
热议问题