Created
July 29, 2021 08:26
-
-
Save niderhoff/bee10d25b63259b8f7350194c8156e20 to your computer and use it in GitHub Desktop.
auto venv bash
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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