Environments, Packaging and Deployment

Main point

  • Standard Project Structure

  • Libraries VS Apps

  • Poetry for Dependency Management

  • Poetry for Building and Publishing to PyPi-compatible services

  • Continuous Testing Workflows

  • Deployment: Github, Custom PyPi Repos, Docker Images

Poetry Setup

# switch to super user if needed
# sudo su

# essential python packages
apt update && apt install python3 python3-venv python-is-python3

# some libraries might need this
apt update && apt install python3-dev

mkdir -p /poetry

export POETRY_HOME=/poetry
export POETRY_VERSION=1.1.13

curl -sSL https://install.python-poetry.org | python3 - --version $POETRY_VERSION

# allows multiple system user to share the same installation
ln -sf /poetry/bin/poetry /usr/local/bin


# bash completions
mkdir -p /etc/bash_completion.d
poetry completions bash | tee /etc/bash_completion.d/poetry


# poetry config
################

# keep venv localized to allowed for root ownership and regular user to run
poetry config virtualenvs.in-project true

# Configure private repository "plataux" with username "mk" and given password
poetry config http-basic.plataux mk '12d@#F34g'

# setting the repo in pyproject.toml like so
# [[tool.poetry.source]]
# name = "plataux"
# url = "https://pypi.tpad.plataux.com"

Poetry Dependency Spec

https://python-poetry.org/docs/dependency-specification/

# pyproject.toml

[tool.poetry.dependencies]

# Get the latest revision on the branch named "next"
requests = { git = "https://github.com/kennethreitz/requests.git", branch = "next" }

# Get a revision by its commit hash
flask = { git = "https://github.com/pallets/flask.git", rev = "38eb5d3b" }

# Get a revision by its tag
numpy = { git = "https://github.com/numpy/numpy.git", tag = "v0.13.2" }

# for private repos, you can use the SSH URL instead
cocktail = {git = "git@github.com:plataux/cocktail.git", branch = "main"}

# For optional packages
aiohttp =  { version = "^3.8", extras = [ "speedups" ] }
uvicorn = { version = "^0.17", extras = [ "standard" ] }

# URL Dependency
my-package = { url = "https://example.com/my-package-0.1.0.tar.gz" }

# Other Examples
gunicorn = "^20.1"
uvloop = "*"

Other Poetry Docs

https://python-poetry.org/docs/pyproject/

a Makefile to authenticate, re-build and publish to private repo

# Makefile

SHELL=/bin/bash

clean:
    rm -f dist/*

publish: clean
    # this is redundant, we don't need to provide basic-auth details each time
    @poetry config http-basic.plataux mk '12d@#F34g'
    poetry build
    @poetry publish -q -r plataux || echo "this version has already been published"