Skip to content

Instantly share code, notes, and snippets.

@rmorshea
Created July 15, 2022 23:12
Show Gist options
  • Save rmorshea/cd9f28d0b49a8163ff00f7832f9db049 to your computer and use it in GitHub Desktop.
Save rmorshea/cd9f28d0b49a8163ff00f7832f9db049 to your computer and use it in GitHub Desktop.
Auto activate venv in current directory
#!/bin/bash
# activate Python venv in present dir
function cd() {
if [[ -d ./.venv ]] && [[ $(which python) = $PWD* ]]; then
deactivate
fi
builtin cd $1
if [[ -d ./.venv ]]; then
. ./.venv/bin/activate
fi
}
# also activate if the shell was started in a dir with venv
if [[ -d ./.venv ]]; then
. ./.venv/bin/activate
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment