Skip to content

Instantly share code, notes, and snippets.

@toshism
Last active December 14, 2023 14:02
Show Gist options
  • Save toshism/ed84ca2209eed2680f215208acf93d45 to your computer and use it in GitHub Desktop.
Save toshism/ed84ca2209eed2680f215208acf93d45 to your computer and use it in GitHub Desktop.
Small script to quickly setup new python environments using pyenv and direnv.
#!/usr/bin/env bash
envrc=$(cat << EOF
if [ -z "\${VENV_BASE:-}" ] && [ -n "\${VIRTUAL_ENV:-}" ]; then
VENV_BASE=\$(basename "\${VIRTUAL_ENV}")
fi
export VENV_BASE
unset PS1
EOF
)
setupcfg=$(cat <<EOF
{
"exclude": [ ".virtualenv" ],
"venvPath": ".",
"venv": ".virtualenv"
}
EOF
)
select_python_version () {
readarray -t lines < <(pyenv versions | sed -e 's/^\*//' -e 's/^ *//' -e 's/\([^ ]*\).*/\1/')
lines+=('Install new version')
echo "select python version:"
select choice in "${lines[@]}"; do
[[ -n $choice ]] || { echo "eh? try again." >&2; continue; }
break
done
if [[ "$choice" == "Install new version" ]]; then
install_new_python
fi
}
install_new_python () {
read -p "What version? " choice
pyenv install $choice
choice=$(pyenv latest $choice)
}
# meh.. maybe add this at some point
install_dependencies () {
read -p "Install lsp?" deps
}
select_python_version
pyenv local $choice
python -m venv .virtualenv
echo source .virtualenv/bin/activate > .envrc
echo "$envrc" >> .envrc
echo "$setupcfg" >> setup.cfg
source .virtualenv/bin/activate
# shut pip up
pip install --upgrade pip
# common deps i like to have
pip install 'python-language-server[all]' ipython pyls-mypy pyls-black pyls_isort
direnv allow
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment