Skip to content

Instantly share code, notes, and snippets.

@TechNickAI
Created July 24, 2025 15:31
Show Gist options
  • Save TechNickAI/2c799d1e0ee3344c8b3637b992aee546 to your computer and use it in GitHub Desktop.
Save TechNickAI/2c799d1e0ee3344c8b3637b992aee546 to your computer and use it in GitHub Desktop.
autoworkon for virtualenvs
# Auto-activate virtualenv based on directory name
function auto_workon() {
# Get the basename of the current directory
local dir_name=$(basename "$PWD")
# Check if we have virtualenvwrapper loaded
if command -v workon >/dev/null 2>&1; then
# Check if a virtualenv with this name exists
if [[ -d "$WORKON_HOME/$dir_name" ]]; then
# Check if we're not already in this virtualenv
if [[ "$VIRTUAL_ENV" != "$WORKON_HOME/$dir_name" ]]; then
#echo "🐍 Activating virtualenv: $dir_name"
workon "$dir_name"
fi
fi
fi
}
# Hook into directory changes
autoload -U add-zsh-hook
add-zsh-hook chpwd auto_workon
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment