cd and virtualenv

2025-06-17

This feels like a fun thing to add to your .zshrc file.

cd() {
    builtin cd "$@"
    
    # Deactivate any current venv
    if [[ -n "$VIRTUAL_ENV" ]]; then
        deactivate
    fi
    
    # Check for virtual environment and activate
    if [[ -f ./venv/bin/activate ]]; then
        source ./venv/bin/activate
    elif [[ -f ./.venv/bin/activate ]]; then
        source ./.venv/bin/activate
    fi
}

When this is loaded, every time you enter a new folder, it's gonna check if there is a virtual environment to go ahead and activate. It's a hack, but it might work well enough. The main downside is that sometimes you will have multiple virtual environments for debugging. Still, it feels like it should cover about 90% of all the painful cases where you try to run something and the wrong virtual environment is loaded. It should also play nicely with uv which is a plus.