Skip to content

Instantly share code, notes, and snippets.

@ChrisSwanson
Created August 6, 2022 18:04
Show Gist options
  • Save ChrisSwanson/3da531691535b286d27bc02f347ebe01 to your computer and use it in GitHub Desktop.
Save ChrisSwanson/3da531691535b286d27bc02f347ebe01 to your computer and use it in GitHub Desktop.
automatically activate/deactivate virtualenv if venv in local dir
# automate sourcing virtualenv if venv dir in local dir.
function cd() {
builtin cd "$@"
if [[ -z "$VIRTUAL_ENV" ]] ; then
## If venv folder is found then activate the vitualenv
if [[ -d ./venv ]] ; then
source ./venv/bin/activate
fi
else
## check the current folder belong to earlier VIRTUAL_ENV folder
# if yes then do nothing
# else deactivate
parentdir="$(dirname "$VIRTUAL_ENV")"
if [[ "$PWD"/ != "$parentdir"/* ]] ; then
deactivate
fi
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment