Introduction
In a previous post, pip-tools were discussed as a build tool for managing package dependencies. This post will focus on a relative newcomer: Poetry.
Comparisons
Within an app managed by pip-tools, we will typically find 2 files per environment: a dependency file, e.g. requirements.in
; and a managed lock file, e.g. requirements.txt
. Any dependency updates must be defined in the appropriate dependency file, and all environments must be updated to ensure constraints are properly handled.
Poetry introduces developer-friendly CLI commands to help reduce this churn. A simple `poetry add [package]` is all that is needed to update both the dependency and lock files, as well as install the package into the local environment. Additionally, the use of environment-specific files is instead managed with a single project definition file: pyproject.toml
(see [PEP 518].
Unlike pip-tools, Poetry is intended to be installed system-wide. Multiple installation methods can be found in the documentation. A valid installation can be confirmed by checking the tool’s version from the command line.
Initialization
New projects can be started with a Poetry-defined structure using the `poetry new` command. Existing projects can be initialized with a new pyproject.toml file using the `poetry init` command, which includes an interactive guide. To demonstrate the latter, let’s continue with the app created in the previous post.Confirm or update the default options, and define the dependencies interactively to allow Poetry to add the appropriate requirements to pyproject.toml. Recall: we need fastapi for our production environment, and fastapi[all] for development.
With a valid pyproject.toml
in place, run `poetry install` to have Poetry resolve all (including dev) dependencies and generate a poetry.lock
file. The option to install with “no-dev” packages is also available.
Note: as we already have an up-to-date virtual environment defined, there is nothing new for Poetry to do.
Activated Shell
With Poetry installed, we can easily run commands within the local virtual environment with `poetry run`, even if that environment is not active.
If desired, you can just as easily start a new shell within the virtual environment with `poetry shell`.
Package Management
Assume an update is desired to the existing app such that we need to add a new package dependency: Pendulum. Directly from the command line we can install and update the requirements files with a single command: `poetry add`.
Note: it’s possible to verify installed packages with `poetry show` to view similar output to `pipdeptree`.
With pendulum installed locally, update the existing main.py file to display the time in UTC.
Start our app server with `poetry run` to validate the updates.
(No More) Requirements
With Poetry managing the dependencies, we no longer need the pip-tools files anymore. We can safely `rm *requirements.*` files from the app directory; all of our requirements are defined in the pyproject.toml
and poetry.lock files.
What if we need that requirements.txt file, though? Perhaps it’s used as part of a Docker build in CI/CD, or maybe it gets run by various terraform or sensible scripts during deploy. Regardless, `poetry export` will generate one from the current poetry.lock file.
The JBS Quick Launch Lab
Free Qualified Assessment
Quantify what it will take to implement your next big idea!
Our assessment session will deliver tangible timelines, costs, high-level requirements, and recommend architectures that will work best. Let JBS prove to you and your team why over 24 years of experience matters.