Skip to content

Instantly share code, notes, and snippets.

@danielsamuels
Last active October 4, 2017 11:58
Show Gist options
  • Save danielsamuels/c548bdfe24719b04c8a5a017823a1c83 to your computer and use it in GitHub Desktop.
Save danielsamuels/c548bdfe24719b04c8a5a017823a1c83 to your computer and use it in GitHub Desktop.
Create Python virtual environments
venv() {
PYTHON_2_COMMAND="/usr/local/bin/virtualenv -p python .venv"
PYTHON_3_COMMAND="/usr/local/bin/python3 -m venv .venv"
# Was the Python version defined in the args?
if [ -z "$1" ]; then
# Try to read the Django version from the requirements
DJANGO_VERSION=(`grep '^Django' requirements.txt | python -c 'import sys; req = sys.stdin.read().strip(); print(req.split(".")[1])'`)
if (( DJANGO_VERSION > 10 )); then
echo "Creating a Python 3 virtual environment (read Django version > 10)."
eval "$PYTHON_3_COMMAND"
else
echo "Creating a Python 2 virtual environment (read Django version <= 10)."
eval "$PYTHON_2_COMMAND"
fi
else
if [ "${1:2}" == 2 ]; then # Python 2 venv
echo "Creating a Python 2 virtual environment (manually specified as 2)."
eval "$PYTHON_2_COMMAND"
elif [ "$1" == 3 ]; then
echo "Creating a Python 3 virtual environment (manually specified as 3)."
eval "$PYTHON_3_COMMAND"
fi
fi
source .venv/bin/activate
pip install -r requirements.txt
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment