Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Leechael/3fd4989ed150e773e921 to your computer and use it in GitHub Desktop.
Save Leechael/3fd4989ed150e773e921 to your computer and use it in GitHub Desktop.
#!/bin/bash
# virtualenv-auto-activate.bash
#
# Installation:
# Add this line to your .bashrc or .bash-profile:
#
# source /path/to/virtualenv-auto-activate.bash
#
# Go to your project folder, run "virtualenv .venv", so your project folder
# has a .venv folder at the top level, next to your version control directory.
# For example:
# .
# ├── .git
# │ ├── HEAD
# │ ├── config
# │ ├── description
# │ ├── hooks
# │ ├── info
# │ ├── objects
# │ └── refs
# └── .venv
# ├── bin
# ├── include
# └── lib
#
# The virtualenv will be activated automatically when you enter the directory.
_virtualenv_auto_activate() {
if [ -e ".venv" ]; then
# Check to see if already activated to avoid redundant activating
DIR="$(pwd -P)/.venv"
# Make sure if .venv is a symlink itself, we look up VIRTUAL_ENV appropriately
READLINK="$(readlink $DIR)"
READLINK=${READLINK%/}
#echo $VIRTUAL_ENV
#echo $DIR
#echo $READLINK
if [ "$VIRTUAL_ENV" != "$DIR" ] && ([ "$VIRTUAL_ENV" != "$READLINK" ] || [ "$VIRTUAL_ENV" == "" ]); then
_VENV_NAME=$(basename `pwd`)
echo Activating virtualenv \"$_VENV_NAME\"...
VIRTUAL_ENV_DISABLE_PROMPT=1
source .venv/bin/activate
_OLD_VIRTUAL_PS1="$PS1"
PS1="($_VENV_NAME) $PS1"
export PS1
fi
fi
}
export PROMPT_COMMAND="_virtualenv_auto_activate; $PROMPT_COMMAND"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment