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

后端 未结 9 762
面向向阳花
面向向阳花 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:08

    You should not use any Python 2.6 features inside the script itself. Also, you must do your version check before importing any of the modules requiring a new Python version.

    E.g. start your script like so:

    #!/usr/bin/env python
    import sys
    
    if sys.version_info[0] != 2 or sys.version_info[1] < 6:
        print("This script requires Python version 2.6")
        sys.exit(1)
    
    # rest of script, including real initial imports, here
    

提交回复
热议问题