Skip to content

Instantly share code, notes, and snippets.

@mklymyshyn
Created April 29, 2011 11:46
Show Gist options
  • Save mklymyshyn/948196 to your computer and use it in GitHub Desktop.
Save mklymyshyn/948196 to your computer and use it in GitHub Desktop.
Activation/Deactivation of python virtualenv upon entering a directory
#!/bin/bash
PREVPWD=`pwd`
PREVENV_PATH=
PREV_PS1=
PREV_PATH=
handle_virtualenv(){
if [ "$PWD" != "$PREVPWD" ]; then
PREVPWD="$PWD";
if [ -n "$PREVENV_PATH" ]; then
if [ "`echo "$PWD" | grep -c $PREVENV_PATH`" = "0" ]; then
source $PREVENV_PATH/.venv
echo "> Virtualenv `basename $VIRTUALENV_PATH` deactivated"
PS1=$PREV_PS1
PATH=$PREV_PATH
PREVENV_PATH=
fi
fi
# activate virtualenv dynamically
if [ -e "$PWD/.venv" ] && [ "$PWD" != "$PREVENV_PATH" ]; then
PREV_PS1="$PS1"
PREV_PATH="$PATH"
PREVENV_PATH="$PWD"
source $PWD/.venv
source $VIRTUALENV_PATH/bin/activate
echo "> Virtualenv `basename $VIRTUALENV_PATH` activated"
fi
fi
}
export PROMPT_COMMAND=handle_virtualenv
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment