Created
April 29, 2011 11:46
-
-
Save mklymyshyn/948196 to your computer and use it in GitHub Desktop.
Activation/Deactivation of python virtualenv upon entering a directory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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