poetry
Table of Content
Table of contents
[[poetry]]
https://python-poetry.org/
Poetry is a tool for dependency management and packaging in Python. It allows you to declare the libraries your project depends on and it will manage (install/update) them for you. Poetry offers a lockfile to ensure repeatable installs, and can build your project for distribution.
setup
https://python-poetry.org/docs/#installing-manually
# assuming venv already activated by mise
# upgrade pip first, and then install poetry
pip install -U pip setuptools
pip install poetry
mkdir ~/.zfunc
# generate completion
poetry completions zsh > ~/.zfunc/_poetry
# add these lines in ~/.zshrc and load
fpath += ~/.zfunc
autoload -Uz compinit && compinit
using poetry
On an existing repository, run poetry init
for poetry to generate whatever files it wants. Poetry configuration file pyproject.toml
will be created.
Run poetry install
to install required packages if poetry files already exist in the project.
Run poetry add {package name}
to add required packages, and poetry will install and update pyproject.toml file accordingly.
Run poetry run python {program file}
to run python through poetry.
Run poetry install --no-root
when you cloned the project using poetry on a different machine and poetry will install the packages required.
CI/CD
See the official guide and also one example I did when I built patched mongodb image.
https://python-poetry.org/docs/#ci-recommendations
Dockerfile FROM registry.blink-1x52.net/cache-dockerhub/library/debian:12.4 as build
RUN apt update -y && apt install -y build-essential \
libcurl4-openssl-dev \
liblzma-dev \
libssl-dev \
python-dev-is-python3 \
python3-pip \
python3-venv \
curl \
git \
gcc \
clang \
&& rm -rf /var/lib/apt/lists/*
ARG MONGO_VERSION=7.2.0
RUN mkdir /src && \
curl -o /tmp/mongo.tar.gz -L "https://github.com/mongodb/mongo/archive/refs/tags/r${MONGO_VERSION}.tar.gz" && \
tar xaf /tmp/mongo.tar.gz --strip-components=1 -C /src && \
rm /tmp/mongo.tar.gz
WORKDIR /src
COPY ./o2_patch_r7.2.0.diff /o2_patch.diff
RUN patch -p1 < /o2_patch.diff
# revised RUN
# avoid error, ref) https://github.com/mongodb/mongo/blob/r7.2.0/docs/building.md#python-prerequisites
ENV PYTHON_KEYRING_BACKEND=keyring.backends.null.Keyring
# env
# ref) https://github.com/orgs/python-poetry/discussions/1879#discussioncomment-7284113
ENV PYTHONUNBUFFERED=1 \
# pip
PIP_DISABLE_PIP_VERSION_CHECK=on \
PIP_DEFAULT_TIMEOUT=100 \
\
# Poetry
# https://python-poetry.org/docs/configuration/#using-environment-variables
POETRY_VERSION=1.6.1 \
# make poetry install to this location
POETRY_HOME="/opt/poetry" \
# do not ask any interactive question
POETRY_NO_INTERACTION=1 \
# never create virtual environment automaticly, only use env prepared by us
POETRY_VIRTUALENVS_CREATE=false \
\
# this is where our requirements + virtual environment will live
VIRTUAL_ENV="/venv"
# create venv
RUN python3 -m venv $VIRTUAL_ENV
# set path for poetry and venv
ENV PATH="$POETRY_HOME/bin:$VIRTUAL_ENV/bin:$PATH"
# install poetry - respects $POETRY_VERSION & $POETRY_HOME
# The --mount will mount the buildx cache directory to where
# Poetry and Pip store their cache so that they can re-use it
RUN --mount=type=cache,target=/root/.cache \
curl -sSL https://install.python-poetry.org | python -
# poetry package management
RUN poetry install --no-root --sync
# build
RUN python buildscripts/scons.py install-mongod MONGO_VERSION="${MONGO_VERSION}" --linker=gold -j8 -Wno-interference-size --release --disable-warnings-as-errors ${JOBS_ARG} && \
mv build/install /install && \
strip --strip-debug /install/bin/mongod && \
rm -rf build
FROM registry.blink-1x52.net/cache-dockerhub/library/debian:12.4
RUN apt update -y && \
apt install -y libcurl4 && \
apt-get clean \
&& rm -rf /var/lib/apt/lists/*
COPY --from=build /install/bin/mongo* /usr/local/bin/
RUN mkdir -p /data/db && \
chmod -R 750 /data && \
chown -R 999:999 /data
USER 999
ENTRYPOINT [ "/usr/local/bin/mongod" ]