Skip to content

Instantly share code, notes, and snippets.

@lixxday
Last active January 12, 2026 14:00
Show Gist options
  • Select an option

  • Save lixxday/92df75cf1474c441ae5851bc05694ec4 to your computer and use it in GitHub Desktop.

Select an option

Save lixxday/92df75cf1474c441ae5851bc05694ec4 to your computer and use it in GitHub Desktop.
#!/bin/bash
# virtualenv-auto-activate.sh
#
# Installation:
# Add this line to your .bashrc or .bash-profile:
#
# source /path/to/virtualenv-auto-activate.sh
#
# The virtualenv will be activated automatically when you enter the directory.
# Setup for a poetry project
_virtualenv_auto_activate() {
if [ -e ".venv" ] || [ -e "venv" ]; then
# Check to see if already activated to avoid redundant activating
echo "Checking if virtualenv is already activated..."
if [ "$VIRTUAL_ENV" != "$(pwd -P)/venv" ]; then
_VENV_NAME=$(basename `pwd`)
echo Activating virtualenv \"$_VENV_NAME\"...
VIRTUAL_ENV_DISABLE_PROMPT=1
source $(poetry env info --path)/bin/activate
_OLD_VIRTUAL_PS1="$PS1"
PS1="($_VENV_NAME)$PS1"
export PS1
fi
else
# Not in a project directory so deactivate if necessary
if [ "$VIRTUAL_ENV" != "" ]; then
echo Deactivating virtualenv...
deactivate
PS1="$_OLD_VIRTUAL_PS1"
export PS1
fi
fi
}
# export PROMPT_COMMAND=_virtualenv_auto_activate; $PROMPT_COMMAND
add-zsh-hook chpwd _virtualenv_auto_activate
_virtualenv_auto_activate
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment