setup_python.sh 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #!/usr/bin/env bash
  2. if [ -z ${PYTHON_VER+x} ]; then
  3. # Use this version of the Python interpreter if it was not defined before
  4. PYTHON_VER=2.7.15
  5. fi
  6. if [ -f /opt/pyenv/activate ];
  7. then
  8. source /opt/pyenv/activate
  9. pyenv global $PYTHON_VER || {
  10. echo 'Python' $PYTHON_VER 'is not installed.'
  11. INSTALLED_PY_VERS=$(pyenv versions --bare)
  12. while [ ${#PYTHON_VER} -gt 0 ]
  13. do
  14. echo 'Tring to locate a match for' $PYTHON_VER
  15. for ver in ${INSTALLED_PY_VERS[@]}
  16. do
  17. if [[ $ver == $PYTHON_VER* ]];
  18. then
  19. pyenv global $ver
  20. break 2
  21. fi
  22. done
  23. # Removing last character and trying to find some match.
  24. # For example, if 3.4.8 was selected but isn't installed then it will try to
  25. # find some other installed 3.4.X version, and then some 3.X.X version.
  26. PYTHON_VER=${PYTHON_VER: : -1}
  27. done
  28. }
  29. python --version || {
  30. echo 'No matching Python interpreter is found!'
  31. exit 1
  32. }
  33. elif command -v python -V 1>/dev/null 2>&1;
  34. then
  35. python --version
  36. echo 'No /opt/pyenv/activate exists and Python from path is used.'
  37. else
  38. echo 'No /opt/pyenv/activate exists and no Python interpreter is found!'
  39. exit 1
  40. fi
  41. # add esp-idf local package path to PYTHONPATH so it can be imported directly
  42. export PYTHONPATH="$IDF_PATH/tools:$IDF_PATH/tools/ci/python_packages:$PYTHONPATH"