Skip to content

Instantly share code, notes, and snippets.

@durden
Created June 25, 2011 18:08
Show Gist options
  • Save durden/1046719 to your computer and use it in GitHub Desktop.
Save durden/1046719 to your computer and use it in GitHub Desktop.
Shell script wrapper for easily starting/populating a virtual env
#!/bin/sh
# This is a function so that we can use 'workon' to switch since it will
# refresh the environment and pass it onto the caller. Thus, no reason to run
# another shell after this command or calling workon manually, etc.
start_virtualenv_main() {
# Assume your working in the directory of the project name
curr=`pwd`
proj=`basename $curr`
# Assume curr directory has requirements.pip
requirements="$curr/requirements.pip"
# Default base virtualenv dir to environment variable, but allow argument
# override
env | grep -q VIRTUALENV_DIR
[ $? -eq 0 ] && virtualenv_base=$VIRTUALENV_DIR
while getopts "p:b:r:" opt; do
case $opt in
p)
proj=$OPTARG ;;
b)
virtualenv_base=$OPTARG ;;
r)
requirements=$OPTARG ;;
\?)
echo "Invalid option: -$OPTARG" >&2
echo "Usage: start_virtualenv [-p <project name>] [-b <virtualenv base directory>] [-r <requirements file>]"
return
;;
esac
done
python="/System/Library/Frameworks/Python.framework/Versions/2.6/Resources/Python.app/Contents/MacOS/Python"
virtualenv_dir="$virtualenv_base/$proj/"
echo "Creating virtual environment for $proj in $virtualenv_dir with $requirements"
mkdir -p $virtualenv_dir
virtualenv --no-site-packages -v -p $python $virtualenv_dir
workon $proj
if [ -e $requirements ]; then
pip install -E $virtualenv_dir -v -r $requirements
else
pip install -E $virtualenv_dir -v
fi
}
# Pass all arguments to function (necessary to use getopts)
start_virtualenv_main "$@"
@durden
Copy link
Author

durden commented Jun 25, 2011

Pretty simple, but I really had remembering the arguments, etc. to virtualenv and pip.
This assumes you have virtualenvwrapper installed (no excuse for you if you don't).

TODO:

  • Need for figure out a way to make 'workon' carry it's environment into the calling shell. Currently after running the script everything works well, but you need to type 'workon ' again to start using the virtualenv at your prompt.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment