Skip to content

Instantly share code, notes, and snippets.

  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save longhotsummer/d8ccfd360457bcf1b270 to your computer and use it in GitHub Desktop.
virtualenv-auto-activate
#!/bin/bash
# virtualenv-auto-activate.sh
#
# Installation:
# Add this line to your .bashrc or .bash-profile:
#
# source /path/to/virtualenv-auto-activate.sh
#
# Go to your project folder, run "virtualenv env", so your project folder
# has a env folder at the top level, next to your version control directory.
# For example:
# .
# ├── .git
# │ ├── HEAD
# │ ├── config
# │ ├── description
# │ ├── hooks
# │ ├── info
# │ ├── objects
# │ └── refs
# └── env
# ├── bin
# ├── include
# └── lib
#
# The virtualenv will be activated automatically when you enter the directory.
# Check for virtualenvwrapper
if type workon >/dev/null 2>&1; then
VENV_WRAPPER=true
else
VENV_WRAPPER=false
fi
VENV_DIR="env"
function _virtualenv_auto_activate() {
if [ -e "$VENV_DIR" ]; then
# Check for symlink pointing to virtualenv
if [ -L "$VENV_DIR" ]; then
_VENV_PATH=$(readlink $VENV_DIR)
_VENV_WRAPPER_ACTIVATE=false
# Check for directory containing virtualenv
elif [ -d "$VENV_DIR" ]; then
_VENV_PATH=$(pwd -P)/$VENV_DIR
_VENV_WRAPPER_ACTIVATE=false
# Check for file containing name of virtualenv
elif [ -f "$VENV_DIR" -a $VENV_WRAPPER = "true" ]; then
_VENV_PATH=$WORKON_HOME/$(cat $VENV_DIR)
_VENV_WRAPPER_ACTIVATE=true
else
return
fi
# Check to see if already activated to avoid redundant activating
if [ "$VIRTUAL_ENV" != $_VENV_PATH ]; then
if $_VENV_WRAPPER_ACTIVATE; then
_VENV_NAME=$(basename $_VENV_PATH)
workon $_VENV_NAME
else
_VENV_NAME=$(basename `pwd`)
VIRTUAL_ENV_DISABLE_PROMPT=1
source $VENV_DIR/bin/activate
_OLD_VIRTUAL_PS1="$PS1"
PS1="($_VENV_NAME)$PS1"
export PS1
fi
echo Activated virtualenv \"$_VENV_NAME\".
fi
fi
}
export PROMPT_COMMAND=_virtualenv_auto_activate
if [ -n "$ZSH_VERSION" ]; then
function chpwd() {
_virtualenv_auto_activate
}
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment