Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Install Poetry with pipx #47

Merged
merged 2 commits into from
Oct 26, 2021
Merged

Install Poetry with pipx #47

merged 2 commits into from
Oct 26, 2021

Conversation

br3ndonland
Copy link
Owner

Description

Poetry has introduced some breaking changes recently. To help buffer against these breaking changes, and to promote reproducible Poetry installations, this PR will install Poetry with pipx instead of get-poetry.py.

Changes

GitHub Actions

The following steps will be taken to install Poetry and verify that the correct version is installed:

  1. Set the PIPX_VERSION and POETRY_VERSION environment variables, which will be used to install specific versions of each package.
  2. Set the POETRY_VIRTUALENVS_IN_PROJECT=true environment variable so that Poetry uses a consistent path for the project's virtualenv.
  3. Install pipx with pip, for the appropriate version of Python. pipx is included by default in the GitHub Actions virtual environment, but only for the default Python version, not necessarily the version installed by actions/setup-python.
  4. Install Poetry with pipx, instead of the get-poetry.py and install-poetry.py install scripts.
  5. Install the Poetry project with poetry install as usual.
  6. Test that the Poetry version returned by the poetry -V command matches $POETRY_VERSION.
  7. Test that the project's virtualenv is installed into the expected location.

These steps promote reproducible Poetry installations.

Docker

As explained in python-poetry/poetry#1879 (comment), there are two conflicting conventions to consider when working with Poetry in Docker:

  1. Docker's convention is to not use virtualenvs, because containers themselves provide sufficient isolation.
  2. Poetry's convention is to always use virtualenvs, because of the reasons given in #2822 Add dockerfile install instructions python-poetry/poetry#3209 (comment).

This project previously preferred the Docker convention:

  • Poetry itself was installed with the get-poetry.py script, with the environment variable POETRY_HOME=/opt/poetry used to specify a consistent location for Poetry.
  • poetry install was used with POETRY_VIRTUALENVS_CREATE=false to install the project's packages into the system Python directory.

The conventional Docker approach no longer works because:

  • The old install script get-poetry.py is deprecated and not compatible with Python 3.10.
  • The new install script install-poetry.py has been problematic so far, and Poetry doesn't really test it, so it will likely continue to be problematic.

In the updated approach:

  • ENV PATH=/opt/pipx/bin:/app/.venv/bin:$PATH will be set first to prepare the $PATH.
  • pip will be used to install a pinned version of pipx.
  • pipx will be used to install a pinned version of Poetry, with PIPX_BIN_DIR=/opt/pipx/bin used to specify the location where pipx installs the Poetry command-line application, and PIPX_HOME=/opt/pipx/home used to specify the location for pipx itself.
  • poetry install will be used with POETRY_VIRTUALENVS_CREATE=true, POETRY_VIRTUALENVS_IN_PROJECT=true and WORKDIR /app to install the project's packages into the virtualenv at /app/.venv.

Subsequent python commands will use the executable at app/.venv/bin/python. As long as POETRY_VIRTUALENVS_IN_PROJECT=true and WORKDIR /app are retained, subsequent Poetry commands will use the same virtual environment at /app/.venv.

For a Poetry project with the following directory structure:

  • repo/
    • package/
      • __init__.py
      • main.py
    • Dockerfile
    • poetry.lock
    • pyproject.toml

A minimal Dockerfile could look like this:

ARG PYTHON_VERSION=3.10
FROM python:$PYTHON_VERSION
ARG PIPX_VERSION=0.16.4 POETRY_VERSION=1.1.11
ENV PATH=/opt/pipx/bin:/app/.venv/bin:$PATH PIPX_BIN_DIR=/opt/pipx/bin PIPX_HOME=/opt/pipx/home POETRY_VIRTUALENVS_IN_PROJECT=true
COPY poetry.lock pyproject.toml /app/
WORKDIR /app
RUN python -m pip install --no-cache-dir --upgrade pip "pipx==$PIPX_VERSION" && \
  pipx install "poetry==$POETRY_VERSION" && \
  poetry install --no-dev --no-interaction --no-root
COPY package /app/package
ENTRYPOINT ["python"]
CMD ["-m", "package.main"]

Related

@vercel
Copy link

vercel bot commented Oct 26, 2021

This pull request is being automatically deployed with Vercel (learn more).
To see the status of your deployment, click below or on the icon next to each commit.

🔍 Inspect: https://vercel.com/br3ndonland/inboard/8WLzDUrKnkT4YY5rVWJynKMDBnEV
✅ Preview: https://inboard-git-pipx-poetry-br3ndonland.vercel.app

@codecov
Copy link

codecov bot commented Oct 26, 2021

Codecov Report

Merging #47 (ebd8454) into develop (48ae350) will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff            @@
##           develop       #47   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           10        10           
  Lines          264       264           
=========================================
  Hits           264       264           
Flag Coverage Δ
unit 100.00% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.


Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 48ae350...ebd8454. Read the comment docs.

GitHub Actions

In GitHub Actions, the following steps will be taken to install Poetry
and verify that the correct version is installed:

1. Set the `PIPX_VERSION` and `POETRY_VERSION` environment variables,
   which will be used to install specific versions of each package
2. Install `pipx` with `pip`, for the appropriate version of Python
   (`pipx` is included by default in the GitHub Actions environment,
   but only for the default Python version, not necessarily the version
   installed by actions/setup-python)
3. Install Poetry with `pipx` instead of get-poetry.py/install-poetry.py
4. Test that the output of `poetry -V` matches `$POETRY_VERSION`

---

Docker

There are conflicting conventions when working with Poetry in Docker:

1. Docker's convention is to not use virtualenvs, because containers
  themselves provide sufficient isolation.
2. Poetry's convention is to always use virtualenvs.

This project has previously preferred the Docker convention:

- Poetry itself was installed with the get-poetry.py script, with the
  environment variable `POETRY_HOME=/opt/poetry` used to specify a
  consistent location for Poetry.
- `poetry install` was used with `POETRY_VIRTUALENVS_CREATE=false` to
  install the project's packages into the system Python directory.

The conventional Docker approach no longer works because:

- The old install script get-poetry.py is deprecated and not compatible
  with Python 3.10.
- The new install script install-poetry.py has been problematic so far,
  and Poetry doesn't really test it, so problems will likely continue.

In the updated approach:

- `ENV PATH=/opt/pipx/bin:/app/.venv/bin:$PATH` will prepare `$PATH`.
- `pip` will be used to install a pinned version of `pipx`.
- `pipx` will be used to install a pinned version of Poetry, with
  `PIPX_BIN_DIR=/opt/pipx/bin` used to specify the location where `pipx`
  installs the Poetry command-line application, and
  `PIPX_HOME=/opt/pipx/home` used as the location for `pipx` itself.
- `poetry install` will be used with `POETRY_VIRTUALENVS_CREATE=true`,
  `POETRY_VIRTUALENVS_IN_PROJECT=true` and `WORKDIR /app`
  to install the project's packages into a virtualenv at `/app/.venv`.

Subsequent `python` commands will use `app/.venv/bin/python`. As long as
`POETRY_VIRTUALENVS_IN_PROJECT=true` and `WORKDIR /app` are retained,
subsequent Poetry commands will use the same virtualenv at `/app/.venv`.
@br3ndonland br3ndonland merged commit a4691bf into develop Oct 26, 2021
@br3ndonland br3ndonland deleted the pipx-poetry branch October 26, 2021 03:06
@br3ndonland br3ndonland mentioned this pull request Oct 26, 2021
10 tasks
br3ndonland added a commit to br3ndonland/fastenv that referenced this pull request Nov 1, 2021
br3ndonland added a commit to br3ndonland/template-python that referenced this pull request Nov 2, 2021
br3ndonland added a commit that referenced this pull request Nov 6, 2021
#47
9476d5b
b33af2a

This commit will add the `poetry config --local virtualenvs.create true`
setting to poetry.toml. This setting is true by default, but it will be
added to poetry.toml for consistent configuration.

The environment variable `POETRY_VIRTUALENVS_CREATE` can also be used to
configure this setting. The environment variable value takes precedence.
https://github.com/python-poetry/poetry/blob/10d5559/poetry/config/config.py#L103-L126
When the new pipx install method from #47 is released, projects will
therefore need to remove `POETRY_VIRTUALENVS_CREATE` from Dockerfiles
and other configuration files for compatibility.
br3ndonland added a commit to br3ndonland/algorithms that referenced this pull request Jan 22, 2022
br3ndonland added a commit to br3ndonland/algorithms that referenced this pull request Jan 22, 2022
br3ndonland added a commit to br3ndonland/algorithms that referenced this pull request Jan 22, 2022
br3ndonland added a commit to br3ndonland/algorithms that referenced this pull request Jan 22, 2022
br3ndonland added a commit to br3ndonland/algorithms that referenced this pull request Jan 22, 2022
br3ndonland added a commit to br3ndonland/algorithms that referenced this pull request Jan 22, 2022
br3ndonland added a commit to br3ndonland/algorithms that referenced this pull request Jan 22, 2022
br3ndonland added a commit to br3ndonland/algorithms that referenced this pull request Jan 22, 2022
br3ndonland added a commit to br3ndonland/dotfiles that referenced this pull request Oct 10, 2022
After creating numerous problems for maintainers by cramming breaking
changes into patch releases, Poetry finally released a minor version,
1.2, which should probably have been a major version.

In order to upgrade, each project will need to update its pyproject.toml
(to support groups), update any references to the install script (which
has been moved twice), and make several other associated changes.

This commit will set an upper bound on the `pipx install poetry` command
to avoid upgrading to Poetry 1.2.

br3ndonland/inboard#36
br3ndonland/inboard#44
br3ndonland/inboard#47
https://python-poetry.org/blog/announcing-poetry-1.2.0/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant