heroku / buildpacks-python

Heroku's Cloud Native Buildpack for Python applications.
BSD 3-Clause "New" or "Revised" License
27 stars 3 forks source link

Add initial support for Poetry #261

Closed edmorley closed 1 month ago

edmorley commented 1 month ago

The Python package manager Poetry is now supported for installing app dependencies: https://python-poetry.org

To use Poetry, apps must have a poetry.lock lockfile, which can be created by running poetry lock locally, after adding Poetry config to pyproject.toml (which can be done either manually or by using poetry init). Apps must only have one package manager file (either requirements.txt or poetry.lock, but not both) otherwise the buildpack will abort the build with an error (which will help prevent some of the types of support tickets we see in the classic buildpack with users unknowingly mixing and matching pip + Pipenv).

Poetry is installed into a build-only layer (to reduce the final app image size), so is not available at run-time. The app dependencies are installed into a virtual environment (the same as for pip after #257, for the reasons described in #253), which is on PATH so does not need explicit activation when using the app image. As such, use of poetry run or poetry shell is not required at run-time to use dependencies in the environment.

When using Poetry, pip is not installed (possible thanks to #258), since Poetry includes its own internal vendored copy that it will use instead (for the small number of Poetry operations for which it still calls out to pip, such as package uninstalls).

Both the Poetry and app dependencies layers are cached, however, the Poetry download/wheel cache is not cached, since using it is slower than caching the dependencies layer (for more details see the comments on poetry_dependencies::install_dependencies).

The poetry install --sync command is run using --only main so as to only install the main [tool.poetry.dependencies] dependencies group from pyproject.toml, and not any of the app's other dependency groups (such as test/dev/... groups, eg [tool.poetry.group.test.dependencies]).

I've marked this semver: major since in the (probably unlikely) event there are any early-adopter projects using this CNB that have both a requirements.txt and poetry.lock then this change will cause them to error (until one of the files is deleted).

Relevant Poetry docs:

Work that will be handled later:

Closes #7. GUS-W-9607867. GUS-W-9608286. GUS-W-9608295.