Created
November 3, 2022 14:06
-
-
Save mccutchen/e9c0ba406d4b89147b97c2329d65d740 to your computer and use it in GitHub Desktop.
Example direnv .envrc for Python projects that manages a local virtualenv that updates automatically when requirements.txt changes
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
#!/bin/bash | |
VENV_DIR=".venv" | |
REQUIREMENTS_HASH_FILE="$VENV_DIR/requirements.shasum" | |
REQUIREMENTS_HASH_FUNC="shasum" | |
REQUIREMENTS_HASH_VAL=$($REQUIREMENTS_HASH_FUNC requirements.txt | cut -d ' ' -f 1) | |
function reset_venv() { | |
rm -rf "$VENV_DIR" | |
python3 -m venv "$VENV_DIR" | |
"$VENV_DIR/bin/pip" install -r "requirements.txt" | |
echo -n "$REQUIREMENTS_HASH_VAL" > "$REQUIREMENTS_HASH_FILE" | |
} | |
if [ ! -f "$REQUIREMENTS_HASH_FILE" ]; then | |
echo "setting up new python virtualenv ..." | |
reset_venv | |
elif [ "$(cat $REQUIREMENTS_HASH_FILE)" != "$REQUIREMENTS_HASH_VAL" ]; then | |
echo "python requirements changed, rebuilding virtualenv ..." | |
echo "(found hash $(cat $REQUIREMENTS_HASH_FILE), want hash $REQUIREMENTS_HASH_VAL)" | |
reset_venv | |
fi | |
PATH_add "$VENV_DIR/bin" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment