Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Script to update Python version for projects with pyenv & poetry

update-python.sh

Script to update Python version for projects with pyenv & poetry

Pre-requisites

This script intended to be run for projects which,

  1. Has .pyenv-version file (Python version managed by pyenv)
  2. Can be installed with make install (or make install-api)
  3. Requirements managed by poetry (so pyproject.toml file is present)

Installation

Download update-python.sh script and put it to your PATH. Make script executable as well.

Usage

update-python.sh <PYTHON_VERSION>
#!/bin/bash
#
# Update Python version for the projects with pyenv & poetry.
#
# Usage
# =====
#
# .. code-block:: bash
#
# update-python.sh <PYTHON_VERSION>
#
set -e
version=$1
if [ -z "$version" -o "$version" == "-h" -o "$version" == "--help" ]; then
echo >&2 "Usage: update-python.sh <PYTHON_VERSION>"
exit 1
fi
pwd=$(pwd)
echo "Update Python to $version in $(basename "${pwd}")"
echo "Update pyenv local version"
pyenv local "$version"
echo "Remove previous virtual environment"
rm -rf .install .install-api .venv/
echo "Update pyproject.toml"
sed -i.tmp "s/^python = \".*\"$/python = \"${version}\"/g" pyproject.toml
rm pyproject.toml.tmp
echo "Install project against new Python"
if [ ! -z "$(grep "^install-api:" Makefile)" ]; then
make install-api
else
make install
fi
echo "Run poetry update"
if [ -f "${pwd}/dotenv.sh" ]; then
"${pwd}/dotenv.sh" poetry update
else
poetry update
fi
echo "All OK!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment