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 "$@"
}
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.
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)