Skip to content

Instantly share code, notes, and snippets.

@dorkness-io
Last active June 8, 2020 07:16
Show Gist options
  • Save dorkness-io/365806f88da3b82e1fa17ea0ca77fdc6 to your computer and use it in GitHub Desktop.
Save dorkness-io/365806f88da3b82e1fa17ea0ca77fdc6 to your computer and use it in GitHub Desktop.
A script to simplify the creation of new pyenv-virtualenv environments.
#!/bin/sh
# This script automates most the dirty work in configuring new Python virtual
# environments. It requires that you've installed pyenv and pyenv-virtualenv.
display_usage() {
echo "\nThis script creates and activates pyenv-virtualenv environments."
echo "It requires that you have pyenv and pyenv-virtualenv installed."
echo "\nUsage: $0 <project_name> <python_version>\n"
echo "Example: $0 new_project 3.8.2\n"
}
command_exists() {
command -v "$1" >/dev/null 2>&1
if [ $? != 0 ]
then
echo "\nAborting Operations!"
echo "\nThis script requires but could not find $1."
echo "Please verify that $1 is installed and on your path.\n"
exit 1
fi
}
if [ $# == 0 ]
then
display_usage
exit 1
fi
command_exists pyenv
command_exists pyenv-virtualenv
# We need these for pyenv activate to work!
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
# Ok, let's do the thing!
mkdir $1 && cd $1 # Create project directory
pyenv local $2 # Create localized python install within that directory
pyenv virtualenv venv-$1 # Create virtual environment from project directory
pyenv local venv-$1 && pyenv activate venv-$1 # Activate venv and set local Python
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment