Skip to content

Instantly share code, notes, and snippets.

@massenz
Created March 28, 2013 00:00
Show Gist options
  • Save massenz/5259290 to your computer and use it in GitHub Desktop.
Save massenz/5259290 to your computer and use it in GitHub Desktop.
Derived from: https://gist.github.com/codysoyland/2198913 Enables automatic activation of a virtualenv once one navigates to a directory with a .venv file whose contents are the path to a virtual environment
#!/bin/bash
# venv.sh
#
# Installation:
# Add this line to your .bashrc or .bash-profile:
#
# source /path/to/venv.sh
#
# Whenever a directory has a file called `.venv` its contents
# will be interpreted as the path to a base directory of
# a virtualenv installation, and `cat .venv`/bin/activate will
# be executed (if the current virtual env is not the same)
#
# Originally derived from: https://gist.github.com/codysoyland/2198913
function _virtualenv_auto_activate() {
if [ -e ".venv" ]; then
local -r _VENV_LOC=$(cat .venv)
# Check to see if already activated to avoid redundant activating
if [ "$VIRTUAL_ENV" != "${_VENV_LOC}" ]; then
_VENV_NAME=$(basename ${_VENV_LOC})
echo "Activating virtualenv \"$_VENV_NAME\"..."
source ${_VENV_LOC}/bin/activate
fi
fi
}
export PROMPT_COMMAND=_virtualenv_auto_activate
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment