diff --git a/Makefile b/Makefile index 752fcbb0054..e070c5b2327 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ DOCKERFILE=docker/build_and_push.Dockerfile DOCKERFILE_BACKEND=docker/build_and_push_backend.Dockerfile DOCKERFILE_FRONTEND=docker/frontend/build_and_push_frontend.Dockerfile DOCKER_COMPOSE=docker_example/docker-compose.yml -PYTHON_REQUIRED=$(shell grep '^python[[:space:]]*=' pyproject.toml | sed -n 's/.*"\([^"]*\)".*/\1/p') +PYTHON_REQUIRED=$(shell grep '^requires-python[[:space:]]*=' pyproject.toml | sed -n 's/.*"\([^"]*\)".*/\1/p') RED=\033[0;31m NC=\033[0m # No Color GREEN=\033[0;32m @@ -44,10 +44,7 @@ check_tools: # check if Python version is compatible check_env: ## check if Python version is compatible @chmod +x scripts/setup/check_env.sh - @PYTHON_INSTALLED=$$(scripts/setup/check_env.sh python --version 2>&1 | awk '{print $$2}'); \ - if ! scripts/setup/check_env.sh python -c "import sys; from packaging.specifiers import SpecifierSet; from packaging.version import Version; sys.exit(not SpecifierSet('$(PYTHON_REQUIRED)').contains(Version('$$PYTHON_INSTALLED')))" 2>/dev/null; then \ - echo "$(RED)Error: Python version $$PYTHON_INSTALLED is not compatible with the required version $(PYTHON_REQUIRED). Aborting.$(NC)"; exit 1; \ - fi + @scripts/setup/check_env.sh "$(PYTHON_REQUIRED)" help: ## show this help message @echo '----' diff --git a/scripts/setup/check_env.sh b/scripts/setup/check_env.sh index 8473255c702..af0191a9dfb 100755 --- a/scripts/setup/check_env.sh +++ b/scripts/setup/check_env.sh @@ -1,21 +1,33 @@ #!/bin/bash -# Detect if in a virtual environment (venv or virtualenv) +# Detect and use appropriate Python interpreter from virtual environments if [ -n "$VIRTUAL_ENV" ]; then - exec "$@" -# Detect if in a conda environment + PYTHON_EXEC=python elif [ -n "$CONDA_DEFAULT_ENV" ]; then - exec conda run -n "$CONDA_DEFAULT_ENV" "$@" -# Detect if in a pipenv environment + PYTHON_EXEC=conda run -n "$CONDA_DEFAULT_ENV" python elif [ -f "Pipfile" ]; then - exec pipenv run "$@" -# Detect if in a pyenv environment + PYTHON_EXEC=pipenv run python elif [ -d ".pyenv" ]; then - exec pyenv exec "$@" -# Detect if in a venv environment -elif [ -f "pyvenv.cfg" ]; then - source bin/activate - exec "$@" + PYTHON_EXEC=pyenv exec python else - exec "$@" + PYTHON_EXEC=python fi + +# Check if Python version is compatible +REQUIRED_VERSION=$1 +PYTHON_INSTALLED=$($PYTHON_EXEC -c "import sys; print(sys.version.split()[0])") + +echo "Detected Python version: $PYTHON_INSTALLED" + +$PYTHON_EXEC -c " +import sys +from distutils.version import LooseVersion + +required_version = '$REQUIRED_VERSION' +python_installed = '$PYTHON_INSTALLED' + +min_version, max_version = required_version.replace('>=', '').replace('<', '').split(',') +if not (LooseVersion(min_version) <= LooseVersion(python_installed) < LooseVersion(max_version)): + sys.exit(f'Error: Python version {python_installed} is not compatible with required version {required_version}.') +" || exit 1 +