Skip to content

Instantly share code, notes, and snippets.

@toasterparty
Created October 24, 2025 20:48
Show Gist options
  • Save toasterparty/4f5f70a32d8c30b0d29925eb34251ba7 to your computer and use it in GitHub Desktop.
Save toasterparty/4f5f70a32d8c30b0d29925eb34251ba7 to your computer and use it in GitHub Desktop.
# Usage:
# venv_setup <venv path> [<python version>] [<requirements.txt path>]
#
# If <python version> is left "", then the default python interpreter
# will be used, otherwise provide a number like "3.11"
#
venv_setup() {
local VENV_DIR="$1"
local PY_VER="${2-}"
local REQUIREMENTS_TXT="$3"
local PY="python$PY_VER"
local VENV_PY="$VENV_DIR/bin/python"
"$PY" --version &> /dev/null || (echo "Error: '$PY' not installed" && exit 1)
if ! "$VENV_PY" --version &> /dev/null; then
if [ -d "$VENV_DIR" ]; then
echo "Removing existing virtual enviornment..."
rm -rf "$VENV_DIR"
fi
echo "Creating new virtual enviornment..."
"$PY" -m venv "$VENV_DIR"
fi
if [[ -n "$REQUIREMENTS_TXT" && -f "$REQUIREMENTS_TXT" ]]; then
echo "Updating packages..."
"$VENV_PY" -m pip install -q --upgrade pip
"$VENV_PY" -m pip install -q -r "$REQUIREMENTS_TXT"
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment