detect_python.sh 891 B

123456789101112131415161718192021222324
  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 will set variable ESP_PYTHON to "python" if it is of version 3.
  7. # 2. Otherwise, "python3" will be exported if it exists.
  8. # 3. The script will fall-back to "python" as the last resort and fail if it doesn't exist.
  9. ESP_PYTHON=python
  10. for p_cmd in python python3
  11. do
  12. echo "Checking \"$p_cmd\" ..."
  13. if [ "$($p_cmd -c "import sys; print(sys.version_info.major)")" = 3 ]; then
  14. ESP_PYTHON=$p_cmd
  15. break
  16. fi
  17. done
  18. $ESP_PYTHON --version || { echo "\"$ESP_PYTHON\" is not installed! Please see the documentation for how to install it."; exit 1; }
  19. echo "\"$ESP_PYTHON\" has been detected"