Skip to content

Instantly share code, notes, and snippets.

@bittner
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bittner/8830745 to your computer and use it in GitHub Desktop.
Save bittner/8830745 to your computer and use it in GitHub Desktop.
Automatically activate virtualenv in Python development. (Source this file from within .bashrc)
# From: "Automatically activate virtualenv" by Kurt Neufeld
# URL: http://www.burgundywall.com/tech/automatically-activate-virtualenv/
# Date: 16-Oct-2013
#
# Usage:
#
# 1. Create an empty .venv file in your project home: touch .venv
# 2. Put this file in your home directory: cp .bash_virtenv ~/
# 3. Include it in ~/.bashrc as follows:
#
# if [ -f ~/.bash_virtenv ]; then
# . ~/.bash_virtenv
# fi
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 `which virtualenvwrapper.sh` # Mac only
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment