Skip to content

Instantly share code, notes, and snippets.

@kodelint
Last active December 8, 2016 22:15
Show Gist options
  • Save kodelint/821a719b9c6e86e41e2eaec0c250341f to your computer and use it in GitHub Desktop.
Save kodelint/821a719b9c6e86e41e2eaec0c250341f to your computer and use it in GitHub Desktop.
Auto activate virtualenv
auto_activate_virtualenv() {
current_dir="${PWD##*/}"
if [ -e ~/.virtualenvs/$current_dir ]; then
deactivate >/dev/null 2>&1
source ~/.virtualenvs/$current_dir/bin/activate
else
deactivate >/dev/null 2>&1
fi
}
pyvenv_cd() {
cd "$@" && auto_activate_virtualenv
}
alias cd="pyvenv_cd"
@kodelint
Copy link
Author

kodelint commented Dec 8, 2016

Adding this to your .bashrc or .zshrc will allow to auto_activate_virtualenv. This could be little heavy as it actually runs the script every time you execute cd. There is a scope of improvement.

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