python_version_checker.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536
  1. # SPDX-FileCopyrightText: 2021 Espressif Systems (Shanghai) CO LTD
  2. #
  3. # SPDX-License-Identifier: Apache-2.0
  4. #
  5. # Script for checking the compatibility of the Python interpreter with ESP-IDF.
  6. #
  7. # There are related tools/detect_python.{sh,fish} scripts which are called earlier when the paths are not properly
  8. # set-up and they only intend to prefer the use of Python 3 over Python 2. Why not more? All possible executables
  9. # (python3.6, python3.7, ...) cannot be hardcoded there and at the end, the user is responsible to set-up a system
  10. # where "python" or "python3" of compatible version is available.
  11. import sys
  12. try:
  13. # Python 2 is not supported anymore but still the old way of typing is used here in order to give a nice Python
  14. # version failure and not a typing exception.
  15. from typing import Iterable
  16. except ImportError:
  17. pass
  18. OLDEST_PYTHON_SUPPORTED = (3, 6) # keep it as tuple for comparison with sys.version_info
  19. def _ver_to_str(it): # type: (Iterable) -> str
  20. return '.'.join(str(x) for x in it)
  21. def is_supported(): # type: () -> bool
  22. return sys.version_info[:2] >= OLDEST_PYTHON_SUPPORTED[:2]
  23. def check(): # type: () -> None
  24. if not is_supported():
  25. raise RuntimeError('ESP-IDF supports Python {} or newer but you are using Python {}. Please upgrade your '
  26. 'installation as described in the documentation.'.format(_ver_to_str(OLDEST_PYTHON_SUPPORTED),
  27. _ver_to_str(sys.version_info[:3])))