Created
July 24, 2025 15:31
-
-
Save TechNickAI/2c799d1e0ee3344c8b3637b992aee546 to your computer and use it in GitHub Desktop.
autoworkon for virtualenvs
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
# 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