Created
May 19, 2025 12:26
-
-
Save digulla/78e97db9a0e2d4e8853e254faff68942 to your computer and use it in GitHub Desktop.
Setup pyenv and pipenv from .python-history and Pipfile
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Set up pyenv from .python-version. | |
# If the current folder contains Pipfile, pipenv and all dependencies will also be installed. | |
error() { # msg | |
echo ERROR "$@" 1>&2 | |
exit 1 | |
} | |
containsElement () { # pattern array... | |
local it pattern="$1" | |
shift | |
for it; do [[ "$it" == "$pattern" ]] && return 0; done | |
return 1 | |
} | |
if [[ $(type -t pyenv) == "" ]]; then | |
error "Unable to find pyenv on PATH" | |
fi | |
if [[ ! -e .python-version ]]; then | |
error "Missing $PWD/.python-version" | |
fi | |
required_version=$(pyenv version-name) | |
if [[ "$required_version" == */envs/* ]]; then | |
echo "INFO Detected virtualenv" | |
python_version=${required_version%%/*} | |
echo "INFO Installing base python $python_version" | |
pyenv install --skip-existing "$python_version" || exit 1 | |
venv_name=${required_version##*/} | |
declare -a existing_venvs=( $(pyenv virtualenvs --bare) ) | |
if containsElement "$required_version" "${existing_venvs[@]}"; then | |
echo "INFO venv already exists" | |
else | |
echo "INFO Creating virtualenv $required_version" | |
pyenv virtualenv "$python_version" "$venv_name" || exit 1 | |
fi | |
else | |
echo "INFO Installing python $required_version" | |
pyenv install --skip-existing "$required_version" | |
fi | |
echo "INFO Your python version is:" | |
pyenv exec python -c 'import sys; print(sys.version)' || exit 1 | |
if [[ -e Pipfile ]]; then | |
echo "INFO Detected pipenv." | |
if pyenv exec pip show pipenv > /dev/null; then | |
: | |
else | |
pyenv exec pip install pipenv | |
fi | |
echo "INFO Installing dependencies..." | |
pyenv exec pipenv install --dev | |
fi | |
echo "SUCCESS" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment