Skip to content

Instantly share code, notes, and snippets.

@revolunet
Created April 21, 2013 11:45
Show Gist options
  • Select an option

  • Save revolunet/5429335 to your computer and use it in GitHub Desktop.

Select an option

Save revolunet/5429335 to your computer and use it in GitHub Desktop.
automatic virtualenv discovery & activation on path change.
#
# adapted from http://www.burgundywall.com/tech/automatically-activate-virtualenv
# source this in your .bash_profile and you're done
#
# todo: ability to set a different virtualenv name
#
export PREVPWD=`pwd`
export PREVENV_PATH=
handle_virtualenv(){
if [ "$PWD" != "$PREVPWD" ]; then
PREVPWD="$PWD";
if [ -n "$PREVENV_PATH" ]; then
if [ "`echo "$PWD" | grep -c $PREVENV_PATH`" = "0" ]; then
deactivate
unalias python 2> /dev/null
PREVENV_PATH=
fi
fi
# activate virtualenv dynamically
if [ -e "$PWD/.venv" ] && [ "$PWD" != "$PREVENV_PATH" ]; then
PREVENV_PATH="$PWD"
workon `basename $PWD`
fi
fi
}
export PROMPT_COMMAND=handle_virtualenv
export VIRTUALENVWRAPPER_VIRTUALENV_ARGS='--no-site-packages'
export WORKON_HOME=~/.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment