detect_python.sh 1.3 KB

12345678910111213141516171819202122232425262728
  1. # This file should be sourced, not executed!
  2. #
  3. # This is a helper script for detecting Python executables in the PATH. It is intended to be used for determining
  4. # which Python should be used with idf_tools.py for installing tools and exporting environment variables.
  5. #
  6. # 1. The script is looking for python version same or greater than minimal required version on path
  7. # 2. If required version of python is found it is assigned to environmental variable `ESP_PYTHON`
  8. # 3. If required version of python is not found, script will fail
  9. OLDEST_PYTHON_SUPPORTED_MAJOR=3
  10. OLDEST_PYTHON_SUPPORTED_MINOR=7
  11. for p_cmd in python3 python python3.7 python3.8 python3.9 python3.10 python3.11 python3.12; do
  12. $p_cmd --version >/dev/null 2>&1 || continue
  13. echo "Checking \"$p_cmd\" ..."
  14. $p_cmd -c "import sys; exit(1) if sys.version_info.major < int(\"$OLDEST_PYTHON_SUPPORTED_MAJOR\") else exit(0);" || continue
  15. $p_cmd -c "import sys; exit(1) if sys.version_info.minor < int(\"$OLDEST_PYTHON_SUPPORTED_MINOR\") else exit(0);" || continue
  16. ESP_PYTHON=$p_cmd
  17. break
  18. done
  19. $ESP_PYTHON --version 2>/dev/null || {
  20. echo "Python ${OLDEST_PYTHON_SUPPORTED_MAJOR}.${OLDEST_PYTHON_SUPPORTED_MINOR}+ is not installed! Please see the documentation for how to install it."
  21. exit 1
  22. }
  23. echo "\"$ESP_PYTHON\" has been detected"