From 7ac03bbc52c2e16f643fe7380e1e33e724418eea Mon Sep 17 00:00:00 2001 From: Maxim Shulga Date: Tue, 23 Jun 2026 20:38:20 -0700 Subject: [PATCH] update-python-env: add --local flag for local virtualenv MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Add shebang and proper quoting for variable checks ($1 → "$1") - Fix version detection to use exact match (grep ^VERSION$ not substring) - Implement --local flag to create project-local .venv instead of pyenv-virtualenv - Automatically upgrades pip/setuptools/wheel and installs requirements.txt --- update-python-env.sh | 38 +++++++++++++++++++++++++++++++++++--- 1 file changed, 35 insertions(+), 3 deletions(-) diff --git a/update-python-env.sh b/update-python-env.sh index 0a820d3..a2a005d 100755 --- a/update-python-env.sh +++ b/update-python-env.sh @@ -1,5 +1,19 @@ -if [ -z $1 ]; then - echo "Usage: $0 [ENV_NAME]" +#!/usr/bin/env bash + +if [ -z "$1" ]; then + echo "Usage: $0 [--local] [ENV_NAME]" + echo " --local create .venv in the current project instead of pyenv-virtualenv" + exit 1 +fi + +LOCAL_VENV=false +if [ "$1" = "--local" ]; then + LOCAL_VENV=true + shift +fi + +if [ -z "$1" ]; then + echo "Usage: $0 [--local] [ENV_NAME]" exit 1 fi @@ -15,13 +29,31 @@ if [[ $PROJECT == "" ]]; then fi echo "[*] Checking if required python version $PY_VER is installed" -pyenv versions | grep $PY_VER > /dev/null +pyenv versions --bare | grep "^$PY_VER$" > /dev/null res=$? if [ $res -eq 1 ]; then echo "Python $PY_VER is not installed. Run \"pyenv install $PY_VER\"" exit 1 fi +if [ "$LOCAL_VENV" = true ]; then + echo "[*] Creating local virtualenv (.venv)" + rm -rf .venv + echo "$PY_VER" > .python-version + "$(pyenv prefix "$PY_VER")/bin/python" -m venv .venv || exit 1 + + echo "[*] Updating pip, setuptools and wheel" + .venv/bin/python -m pip install -U pip setuptools wheel || exit 1 + + if [ -f requirements.txt ]; then + echo "[*] Installing packets from requirements.txt" + .venv/bin/python -m pip install -Ur requirements.txt || exit 1 + fi + + echo "[*] Local virtualenv is ready. Activate it with: source .venv/bin/activate" + exit 0 +fi + CUR_VENV=$(cat .python-version) echo "[*] Removing current virtualenv ($CUR_VENV)" pyenv virtualenv-delete -f $CUR_VENV || true