Skip to content

Instantly share code, notes, and snippets.

@tbaumann
Created October 27, 2016 11:22
Show Gist options
  • Select an option

  • Save tbaumann/0954180e98928db7e4dd151eebc25274 to your computer and use it in GitHub Desktop.

Select an option

Save tbaumann/0954180e98928db7e4dd151eebc25274 to your computer and use it in GitHub Desktop.
Enable virtualenv in git repos if .venv is available
## Put this in your .bashrc
function workon_cwd {
if git rev-parse --is-inside-work-tree > /dev/null 2>/dev/null
then
REPO_TOPLEVEL=`git rev-parse --show-cdup`
if [ -f "./$REPO_TOPLEVEL/.venv" ]
then
ENV_NAME=`cat "./$REPO_TOPLEVEL/.venv"`
# Activate the environment only if it is not already active
if [ "$VIRTUAL_ENV" != "$WORKON_HOME/$ENV_NAME" ]
then
workon "$ENV_NAME" && export CD_VIRTUAL_ENV="$ENV_NAME"
fi
fi
elif [ $CD_VIRTUAL_ENV ]; then
# We've just left the repo, deactivate the environment
# Note: this only happens if the virtualenv was activated automatically
deactivate && unset CD_VIRTUAL_ENV
fi
}
# Might conflict with other cd implemntations. Perhaps don't call builtin cd but whatever other cd implementation also exists
function cd {
builtin cd "$@" && workon_cwd
}
workon_cwd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment