Kaizen Today I Learned by Ville Säävuori

Optional Dependencies With Poetry

Some Python dependencies require build tools which can be problematic when running inside Docker or CI environment. Often these packages are also needed only in development or production so having them as optional dependencies can be really useful. I just learned that Poetry can do this.

First, install your dependency with --optional flag:

poetry add -D pywatchman --optional

Not sure if this step is needed, YMMV, but I needed to add a new section in the pyproject.toml file and run poetry update after:

pywatchman = ["pywatchman"]

and now, when you do a normall install, the optional packages are not installed. If you want to install those, run:

poetry install -E pywatchman

This is very handy as I now can easily keep things like pywatchman and uwsgi in my Poetry deps without worrying about their build dependencies in CI and/or local environments.

As a very long time user of pip-tools, I really like Poetry as a modern alternative.

Tagged with , ,

Published . Last modified .