DEV Community

Benji πŸ™
Benji πŸ™

Posted on

My sudden episode to keep my virtualenv in check

I often run python scripts or commands outside of a virtualenv and this creates a problem with dependencies being installed globally, often left unchecked with wrong python versions.

The solution is simple; If i'm not running a virtualenv anywhere then ideally I should get a warning message, loudly. What i've ended up doing is creating a wrapper in my zsh config file that looks something like below:

# Python wrapper to warn about uv venv
python() {
environment
    if [ -z "$VIRTUAL_ENV" ] || [ ! -f "$VIRTUAL_ENV/pyvenv.cfg" ] || ! grep -q "uv" "$VIRTUAL_ENV/pyvenv.cfg" 2>/dev/null; then
        echo "\033[1;31m" >&2  # Bold Red
        echo "" >&2
        echo " β–ˆβ–ˆβ•—    β–ˆβ–ˆβ•— β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•— β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•— β–ˆβ–ˆβ–ˆβ•—   β–ˆβ–ˆβ•—β–ˆβ–ˆβ•—β–ˆβ–ˆβ–ˆβ•—   β–ˆβ–ˆβ•— β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•— β–ˆβ–ˆβ•—" >&2
        echo " β–ˆβ–ˆβ•‘    β–ˆβ–ˆβ•‘β–ˆβ–ˆβ•”β•β•β–ˆβ–ˆβ•—β–ˆβ–ˆβ•”β•β•β–ˆβ–ˆβ•—β–ˆβ–ˆβ–ˆβ–ˆβ•—  β–ˆβ–ˆβ•‘β–ˆβ–ˆβ•‘β–ˆβ–ˆβ–ˆβ–ˆβ•—  β–ˆβ–ˆβ•‘β–ˆβ–ˆβ•”β•β•β•β•β• β–ˆβ–ˆβ•‘" >&2
        echo " β–ˆβ–ˆβ•‘ β–ˆβ•— β–ˆβ–ˆβ•‘β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•‘β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•”β•β–ˆβ–ˆβ•”β–ˆβ–ˆβ•— β–ˆβ–ˆβ•‘β–ˆβ–ˆβ•‘β–ˆβ–ˆβ•”β–ˆβ–ˆβ•— β–ˆβ–ˆβ•‘β–ˆβ–ˆβ•‘  β–ˆβ–ˆβ–ˆβ•—β–ˆβ–ˆβ•‘" >&2
        echo " β–ˆβ–ˆβ•‘β–ˆβ–ˆβ–ˆβ•—β–ˆβ–ˆβ•‘β–ˆβ–ˆβ•”β•β•β–ˆβ–ˆβ•‘β–ˆβ–ˆβ•”β•β•β–ˆβ–ˆβ•—β–ˆβ–ˆβ•‘β•šβ–ˆβ–ˆβ•—β–ˆβ–ˆβ•‘β–ˆβ–ˆβ•‘β–ˆβ–ˆβ•‘β•šβ–ˆβ–ˆβ•—β–ˆβ–ˆβ•‘β–ˆβ–ˆβ•‘   β–ˆβ–ˆβ•‘β•šβ•β•" >&2
        echo " β•šβ–ˆβ–ˆβ–ˆβ•”β–ˆβ–ˆβ–ˆβ•”β•β–ˆβ–ˆβ•‘  β–ˆβ–ˆβ•‘β–ˆβ–ˆβ•‘  β–ˆβ–ˆβ•‘β–ˆβ–ˆβ•‘ β•šβ–ˆβ–ˆβ–ˆβ–ˆβ•‘β–ˆβ–ˆβ•‘β–ˆβ–ˆβ•‘ β•šβ–ˆβ–ˆβ–ˆβ–ˆβ•‘β•šβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•”β• β–ˆβ–ˆβ•—" >&2
        echo "  β•šβ•β•β•β•šβ•β•β• β•šβ•β•  β•šβ•β•β•šβ•β•  β•šβ•β•β•šβ•β•  β•šβ•β•β•β•β•šβ•β•β•šβ•β•  β•šβ•β•β•β• β•šβ•β•β•β•β•β•  β•šβ•β•" >&2
        echo "" >&2
        echo "\033[1;33m UV virtual environment is NOT activated!\033[0m" >&2
        echo "\033[0;36m Run: uv venv && source .venv/bin/activate\033[0m" >&2
        echo "" >&2
        return 1
    fi

    # Execute the actual python command with all arguments
    command python "$@"
}
Enter fullscreen mode Exit fullscreen mode

The above wrapper is specifically designed for uv-managed environments, checking not just if $VIRTUAL_ENV is set but also validating that it's actually a uv environment by grepping the pyvenv.cfg file.

Whenever i forget to activate my virtualenv, i'll get a massive ASCII art "WARNING!" banner in bold red, followed by instructions in yellow and cyan. It's obnoxious in the best possible wayβ€”you literally cannot miss it.

Execution of the python wrapper

IMO the beauty in this is the Docker-esque approach so anytime I'm done with an environment, or I want to use a different Python version I can just deactivate and remove the current .venv module and start again.

Sometimes the best development tools are the ones that yell at you when you're about to do something really dumb or just out of impulse.

Top comments (0)