Skip to content

Instantly share code, notes, and snippets.

@mattjmorrison
Created April 15, 2012 23:02
Show Gist options
  • Save mattjmorrison/2395220 to your computer and use it in GitHub Desktop.
Save mattjmorrison/2395220 to your computer and use it in GitHub Desktop.
@toranb's Automatically 'workon' a virtualenv
# This makes 3 assumptions
# 1.) the root project directory must have a .git file (this way it won't try to "workon" in every single
# directory on your machine)
# 2.) the virtualenv name for your project must match the root directory name of your project
# 3.) you want it to auto switch virtualenvs for you when you jump between different project directories
# (when you hit another dir w/ a virtualenv that is)
workon_virtualenv() {
if [ -e .git ]; then
current_dir="${PWD##*/}"
if [ -e ~/virtualenvs/$current_dir ]; then
deactivate >/dev/null 2>&1
source ~/virtualenvs/$current_dir/bin/activate
fi
fi
}
virtualenv_cd() {
cd "$@" && workon_virtualenv
}
alias cd="virtualenv_cd"
@brouberol
Copy link

This is great! A small suggestion: why not using $WORKON_HOME instead of hardcoding ~/virtualenvs/ as the directory containing virtualenvs?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment