Skip to content

Instantly share code, notes, and snippets.

@larsyencken
Created September 15, 2022 12:13
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 larsyencken/c1b2d161e7102e2ad569a735fe088abe to your computer and use it in GitHub Desktop.
Save larsyencken/c1b2d161e7102e2ad569a735fe088abe to your computer and use it in GitHub Desktop.
zsh: auto-enter python virtualenvs on cd
#
# python: enter virtualenv on chpwd
#
load-py-venv() {
if [ -f .venv/bin/activate ]; then
# enter a virtual environment that's here
source .venv/bin/activate
elif [ -f env/bin/activate ]; then
source env/bin/activate
elif [ ! -z "$VIRTUAL_ENV" ] && [ -f pyproject.toml -o -f requirements.txt ]; then
# exit a virtual environment when you enter a new project folder
deactivate
fi
}
add-zsh-hook chpwd load-py-venv
load-py-venv
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment