Skip to content

Instantly share code, notes, and snippets.

@niderhoff
Created July 29, 2021 08:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save niderhoff/bee10d25b63259b8f7350194c8156e20 to your computer and use it in GitHub Desktop.
Save niderhoff/bee10d25b63259b8f7350194c8156e20 to your computer and use it in GitHub Desktop.
auto venv bash
# venv
function deactivate_venv() {
parentdir=$(dirname "${VIRTUAL_ENV}")
if [[ "$PWD"/ != "$parentdir"/* && -n "$(command -v deactivate)" ]] ; then
echo "Deactivating ${VIRTUAL_ENV}"
deactivate
activate_venv
fi
}
function activate_venv() {
if [[ -f "$PWD/${DEFAULT_ENV_PATH}/bin/activate" ]] ; then~
source "$PWD/${DEFAULT_ENV_PATH}/bin/activate"
echo "Activating ${VIRTUAL_ENV}"
fi
}
function auto_activate_env() {
builtin cd "$@" || return
## Default path to virtualenv in your projects
DEFAULT_ENV_PATH="/.venv"
## If env folder is found then activate the vitualenv
if [[ -z "$VIRTUAL_ENV" ]] ; then
activate_venv
else
deactivate_venv
fi
}
alias cd="auto_activate_env"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment