Skip to content

Instantly share code, notes, and snippets.

@4r7ur-sid
Last active November 9, 2024 17:58
Show Gist options
  • Save 4r7ur-sid/84265e40fd7650b755646e74700a05cb to your computer and use it in GitHub Desktop.
Save 4r7ur-sid/84265e40fd7650b755646e74700a05cb to your computer and use it in GitHub Desktop.
Oh My ZSH. Automatically Turn ON/Off Virtual Environment
# Function to activate virtual environment
function auto_activate_env() {
# Check for the presence of your custom environment file, e.g., `.venvfile`
if [ -f ".venvfile" ]; then
# Read the full path to the activate script from .venvfile
ENV_PATH=$(cat .venvfile)
if [ -f "$ENV_PATH" ]; then
# Check if already activated to prevent redundant activations
if [[ "$VIRTUAL_ENV" != "$(dirname "$ENV_PATH")" ]]; then
source "$ENV_PATH"
echo "Activated virtual environment at: $ENV_PATH"
# Set OLDPWD to prevent immediate deactivation
OLDPWD=$PWD
fi
else
echo "Activation script '$ENV_PATH' not found."
fi
fi
}
# Hook function to deactivate the virtual environment only when leaving the env root or its subdirectories
function auto_deactivate_env() {
if [ -n "$VIRTUAL_ENV" ]; then
ENV_ROOT=$(dirname "$VIRTUAL_ENV")
# Check if the current directory is outside the environment root and its subdirectories
if [[ "$PWD" != "$ENV_ROOT"* ]]; then
deactivate
echo "Deactivated virtual environment"
fi
fi
}
# Trap functions to call on directory change
autoload -U add-zsh-hook
add-zsh-hook chpwd auto_activate_env
add-zsh-hook chpwd auto_deactivate_env
# Initial call to activate environment if starting in a directory with .venvfile
auto_activate_env
# Read this 👇
# To trigger the environment in any folder, you need to have a .venvfile containing the absolute path to the virtual environment.
# EG: /Users/4r7ur/Developer/bi/alpha/bi/bin/activate here the last bi was the virtual environment
@4r7ur-sid
Copy link
Author

Screenshot 2024-11-09 at 23 25 52

@4r7ur-sid
Copy link
Author

Auto Virtual Environment Activation and Deactivation Script

This bash script automates the activation and deactivation of Python virtual environments based on the current directory. It simplifies workflows for projects with different environments.


Script Explanation

1. Function: auto_activate_env

This function activates a virtual environment when you enter a directory containing a .venvfile.

  • Check for .venvfile:
    The script looks for a .venvfile in the current directory. This file must contain the absolute path to the activate script of the virtual environment.

  • Read the environment path:
    If .venvfile exists, the script reads its contents to get the path of the virtual environment activation script.

  • Check if the environment is already activated:
    If the specified virtual environment is not currently active (checked via $VIRTUAL_ENV), it activates it using the source command.

  • Set $OLDPWD:
    Updates $OLDPWD to the current directory to prevent accidental deactivation when moving directories.

  • Error handling:
    If the activate script mentioned in .venvfile is not found, an error message is displayed.


2. Function: auto_deactivate_env

This function deactivates the virtual environment when you leave the directory of the environment or its subdirectories.

  • Check if a virtual environment is active:
    If $VIRTUAL_ENV is set, it means a virtual environment is active.

  • Determine the root of the environment:
    The root directory of the environment is determined from $VIRTUAL_ENV.

  • Deactivate conditionally:
    If the current working directory ($PWD) is outside the environment's root directory (or its subdirectories), the environment is deactivated using the deactivate command.


3. Directory Change Hooks

The script uses directory change hooks in zsh to call the activation and deactivation functions whenever you change directories.

  • add-zsh-hook:
    Adds hooks for the chpwd event, which triggers whenever the current directory changes.

  • Linking the hooks:
    The auto_activate_env and auto_deactivate_env functions are executed every time you change directories.


4. Initial Call

When the shell starts, the auto_activate_env function is called immediately to activate the environment if the starting directory contains a .venvfile.


5. .venvfile Requirement

To make this work in any project folder:

  • Add a .venvfile to the directory.
  • The .venvfile should contain the absolute path to the activate script of the virtual environment (e.g., /path/to/your/venv/bin/activate).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment