Skip to content

Instantly share code, notes, and snippets.

@aymenkrifa
Created April 29, 2024 15:50
Show Gist options
  • Save aymenkrifa/b66c13618267cc3b40e76c40b78f0a59 to your computer and use it in GitHub Desktop.
Save aymenkrifa/b66c13618267cc3b40e76c40b78f0a59 to your computer and use it in GitHub Desktop.
# Overrides 'pip' based on the virtual environment.
# Uses 'uv pip' if the environment was created by 'uv', otherwise uses 'pip'.
function pip() {
RED='\033[0;31m'
GREEN='\033[0;32m'
BOLD='\033[1m'
NC='\033[0m'
if [[ -n "$VIRTUAL_ENV" ]]; then
if [[ -f "$VIRTUAL_ENV/pyvenv.cfg" ]]; then
if grep -q "uv" "$VIRTUAL_ENV/pyvenv.cfg"; then
if command -v uv > /dev/null 2>&1; then
echo -e "${GREEN}Running ${BOLD}'uv pip $@'${NC}"
uv pip "$@"
else
echo -e "${RED}'uv' command not found. Running ${BOLD}'pip $@'${NC}"
command pip "$@"
fi
else
echo -e "${GREEN}Running ${BOLD}'pip $@'${NC}"
command pip "$@"
fi
else
echo -e "${RED}pyvenv.cfg file does not exist. Running ${BOLD}'pip $@'${NC}"
command pip "$@"
fi
else
echo -e "${RED}No virtual environment is activated. Running ${BOLD}'pip $@'${NC}"
command pip "$@"
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment