Shell function to help with Python’s Virtual Environment

Permalink to section "Shell function to help with Python’s Virtual Environment"

Managing Python projects often involves juggling multiple dependencies and Python versions. Virtual environments are a lifesaver, allowing you to isolate project dependencies and avoid conflicts. While Python’s venv module is excellent, creating and activating environments can be a bit repetitive. To streamline this process, I’ve created a simple Bash function called ve() that simplifies virtual environment management:

ve() {
    local py="${1:-python}"
    local venv="${2:-.venv}"
    local venv_path="./${venv}"
    local activate_script="${venv_path}/bin/activate"

    # Already inside a virtual environment?
    if [[ -n "$VIRTUAL_ENV" ]]; then
        echo "Already in a virtual environment: $VIRTUAL_ENV"
        return 0
    fi

    # Prefer uv if installed
    if command -v uv >/dev/null 2>&1; then
        if [[ ! -d "$venv_path" ]]; then
            echo "Creating virtual environment with uv: $venv_path"
            uv venv "$venv_path" --python "$py" || return 1
        else
            echo "Using existing uv virtual environment: $venv_path"
        fi
    else
        # Fallback to Python venv
        if [[ ! -d "$venv_path" ]]; then
            echo "Creating virtual environment with python: $venv_path"
            "$py" -m venv "$venv_path" --system-site-packages || return 1

            echo "export PYTHON=$py" >> "$activate_script"

            echo "Upgrading pip in new virtual environment..."
            source "$activate_script"
            "$py" -m pip install --upgrade pip || return 1
            deactivate
            echo "Virtual environment created and pip upgraded."
        else
            echo "Activating existing python virtual environment: $venv_path"
        fi
    fi

    # Activate regardless of method
    source "$activate_script" || return 1
    echo "Virtual environment activated: $venv_path"
}

The function first checks if a virtual environment is already active by looking for the VIRTUAL_ENV environment variable. If we’re in a virtual environment already, the script will exit gracefully.

If we are not in a virtual environment AND there’s no .venv folder found in the current directory, it will create a new virtual environment and then activates the environment.

Finally, it upgrades pip within the new virtual environment to ensure you have the latest version.

You can add this function to your shell configuration like .bashrc, .zshrc, or equivalent shell configuration file. This will make the ve() function available in your terminal.