Last active
March 16, 2019 09:46
-
-
Save x42en/2c5dcc57b5c94a83647fa8a148bb9d82 to your computer and use it in GitHub Desktop.
Python virtual environment (pyenv + pipenv) installation - tested on debian 9 only
This file contains 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 | |
# Setup constants | |
PYTHON_DEFAULT="3.6.8" | |
# Pickup some version from "pyenv install --list" | |
# Careful some version are not working depending of you hardware/OS target | |
PYTHON_VERSIONS=("2.5.6","2.7.12","2.7.15","3.5.3","3.5.6","3.6.8","3.7.2","3.8-dev") | |
# Detect bash version | |
if [[ -n "$ZSH_VERSION" ]]; then | |
PROFILE="$HOME/.zshrc" | |
elif [[ -n "$BASH_VERSION" ]]; then | |
PROFILE="$HOME/.bashrc" | |
fi | |
# If no version set use default | |
value=${1:-$PYTHON_DEFAULT} | |
# Install pyenv and pipenv based on version given | |
if [[ "${PYTHON_VERSIONS[@]}" =~ "${value}" ]]; then | |
PYTHON_VERSION=$value | |
else | |
echo -e "[!] This python version is not a valid candidate, we will install python v$PYTHON_DEFAULT instead..." | |
echo '[!] Would you like to continue?' | |
select yn in "Yes" "No"; do | |
case $yn in | |
Yes ) break;; | |
No ) echo "Good bye"; exit 1;; | |
esac | |
done | |
PYTHON_VERSION=$PYTHON_DEFAULT | |
fi | |
echo "[+] Installing virtual environment based on ${PYTHON_VERSION}" | |
# run pyenv install as current user: | |
echo "[+] Download pyenv install script" | |
curl https://pyenv.run | bash | |
# Add virtual environment to you shell by adding following lines in your .bashrc | |
echo "[+] Setup user environment" | |
echo -e '\n# Activate virtual environment for python' >> $PROFILE | |
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> $PROFILE | |
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> $PROFILE | |
echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n eval "$(pyenv init -)"\nfi' >> $PROFILE | |
# Update user shell | |
echo "[+] Update current shell" | |
export PYENV_ROOT="${HOME}/.pyenv" | |
export PATH="${PYENV_ROOT}/bin:${PATH}" | |
if command -v pyenv 1>/dev/null 2>&1; then | |
eval "$(pyenv init -)" | |
fi | |
# Install needed python versions | |
echo "[+] Install virtual python (${PYTHON_VERSION})" | |
pyenv install $PYTHON_VERSION | |
# Activate python version as current shell | |
echo "[+] Activate python shell" | |
pyenv shell $PYTHON_VERSION | |
# Install pipenv for user | |
export PATH="${HOME}/.local/bin:${PATH}" | |
echo "[+] Install pipenv for user" | |
pip install --user pipenv | |
# Activate pipenv for user | |
echo "[+] Activate pipenv" | |
echo -e '\n# Activate virtual environment for pip' >> $PROFILE | |
echo 'export PIPENV_PYTHON="$PYENV_ROOT/shims/python"' >> $PROFILE | |
echo 'export PATH="$HOME/.local/bin:$PATH"' >> $PROFILE | |
echo "[+] All done..." | |
echo "[+] Move to your project, and use pipenv by typing: 'pipenv install'" | |
# Restart shell to get current values | |
exec $SHELL |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment