Created
July 14, 2024 10:45
-
-
Save amtoine/740aaa0e0091e6a089d5f3e71fa162e3 to your computer and use it in GitHub Desktop.
Direnv script to manage Python virtual environments
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export PYTHONPATH=$(pwd) | |
RED='\033[0;31m' | |
GREEN='\033[0;32m' | |
PURPLE='\033[0;35m' | |
YELLOW='\033[1;33m' | |
NC='\033[0m' | |
error () { printf "${RED}[!]${NC} ${1}\n"; } | |
info () { printf "${GREEN}[~]${NC} ${1}\n"; } | |
warning () { printf "${YELLOW}[?]${NC} ${1}\n"; } | |
fmt-file () { echo "${PURPLE}${1}${NC}"; } | |
if [ ! -f ".venv/bin/activate" ]; then | |
error "Creating virtual environment in $(fmt-file .venv/)" | |
virtualenv .venv | |
fi | |
info "Activating virtual environment from $(fmt-file .venv/)" | |
source .venv/bin/activate | |
unset PS1 | |
if [ ! -f "requirements.txt" ]; then | |
warning "no $(fmt-file requirements.txt), skipping Python version check" | |
exit 0 | |
fi | |
e=$(grep '^# python==' requirements.txt | sed 's/# python==//') | |
if [ -z "$e" ]; then | |
warning "no Python version in $(fmt-file requirements.txt), skipping Python version check" | |
exit 0 | |
fi | |
v=$(python --version | sed 's/^Python //') | |
if [ "$e" = "$v" ]; then | |
info "Version is ${v}" | |
else | |
warning "Version should be ${e}, found ${v}" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment