Python poetry - how to install optional dependencies?

青春壹個敷衍的年華 提交于 2020-11-29 04:26:48

问题


Python's poetry dependency manager allows specifying optional dependencies via command:

$ poetry add --optional redis

Which results in this configuration:

[tool.poetry.dependencies]
python = "^3.8"
redis = {version="^3.4.1", optional=true}

However how do you actually install them? Docs seem to hint to:

$ poetry install -E redis

but that just throws and error:

Installing dependencies from lock file

[ValueError]
Extra [redis] is not specified.

回答1:


You need to add a tool.poetry.extras group to your pyproject.toml if you want to use the -E flag during install, as described in this section of the docs:

[tool.poetry.extras]
caching = ["redis"]

The key refers to the word that you use with poetry install -E, and the value is a list of packages that were marked as --optional when they were added. There currently is no support for making optional packages part of a specific group during their addition, so you have to maintain it by hand.

The reason behind this additional layer of abstraction is that extra-installs usually refer to some additional functionality that is enabled through the installation of one or more dependencies. poetry simply mimics setuptools' definition of extra-installs here, which might explain why it's so sparingly documented.



来源:https://stackoverflow.com/questions/60971502/python-poetry-how-to-install-optional-dependencies

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!