简体   繁体   English

Python 诗——如何安装可选依赖项?

[英]Python poetry - how to install optional dependencies?

Python's poetry dependency manager allows specifying optional dependencies via command: Python 的诗歌依赖管理器允许通过命令指定可选依赖项:

$ 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.

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:如果您想在安装期间使用-E标志,您需要将tool.poetry.extras组添加到您的pyproject.toml ,如文档的本节所述:

[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.键是指您与poetry install -E一起使用的词,值是添加时标记为--optional的包的列表。 There currently is no support for making optional packages part of a specific group during their addition, so you have to maintain this section in your pyproject.toml file by hand.当前不支持在添加过程pyproject.toml可选包作为特定组的一部分,因此您必须手动在pyproject.toml文件中维护此部分。

The reason behind this additional layer of abstraction is that extra-installs usually refer to some optional functionality (in this case caching ) that is enabled through the installation of one or more dependencies (in this case just redis ).这个额外的抽象层背后的原因是额外安装通常指的是通过安装一个或多个依赖项(在这种情况下只是redis )启用的一些可选功能(在这种情况下是caching )。 poetry simply mimics setuptools ' definition of extra-installs here, which might explain why it's so sparingly documented. poetry在这里简单地模仿了setuptools对额外安装的定义,这可能解释了为什么它的文档如此之少。

I will add that not only you have to have this extras section added by hand, as well your optional dependencies cannot be in dev section.我要补充的是,不仅您必须手动添加这个 extras 部分,而且您的可选依赖项不能在 dev 部分中。

Example of code that won't work:的代码示例将无法正常工作:

[tool.poetry]
name = "yolo"
version = "1.0.0"
description = ""
authors = []

[tool.poetry.dependencies]
python = "2.7"
Django = "*"

[tool.poetry.dev-dependencies]
pytest = "*"
ipdb = {version = "*", optional = true}

[tool.poetry.extras]
dev_tools = ["ipdb"]

But this WILL work:但这起作用:

[tool.poetry]
name = "yolo"
version = "1.0.0"
description = ""
authors = []

[tool.poetry.dependencies]
python = "2.7"
Django = "*"
ipdb = {version = "*", optional = true}

[tool.poetry.dev-dependencies]
pytest = "*"

[tool.poetry.extras]
dev_tools = ["ipdb"]

Up-voted Drachenfels's answer.投票赞成 Drachenfels 的回答。

Dev dependency could not be optional , otherwise, no matter how you tweak it with extras or retry with poetry install -E , it will just never get installed.开发依赖不能是可选的,否则,无论您如何使用附加功能调整它或使用poetry install -E重试,它都永远不会安装。

This sounds like a bug but somehow by design ,这听起来像一个错误,但不知何故是设计使然

...this is not something I want to add. ...这不是我要添加的内容。 Extras will be referenced in the distributions metadata when packaging the project but development dependencies do not which will lead to a broken extras.打包项目时,将在分发元数据中引用 Extras,但开发依赖项不会,这将导致 Extras 损坏。

— concluded in Poetry PR#606 comment by one maintainer. — 一位维护者在 Poetry PR#606 评论中得出结论。 See here for detailed context: https://github.com/python-poetry/poetry/pull/606#issuecomment-437943927有关详细信息,请参见此处: https://github.com/python-poetry/poetry/pull/606#issuecomment-437943927


I would say that I can accept the fact that optional dev-dependency cannot be implemented.我会说我可以接受无法实现可选开发依赖的事实。 However, at least Poetry should warn me when I have such a config.但是,当我有这样的配置时,至少 Poetry 应该警告我。 If so, I wouldn't have been confused for a long time, reading each corner of the help manual and found nothing helpful.如果是这样,我就不会困惑很长时间,阅读帮助手册的每个角落并没有发现任何帮助。


I found some people did get trap in this problem ( Is poetry ignoring extras or pyproject.toml is misconfigured? ) but their questions are closed, marked duplicated and re-linked to this question.我发现有些人确实陷入了这个问题( 诗歌是否忽略了额外内容或 pyproject.toml 配置错误? )但他们的问题已关闭,标记为重复并重新链接到此问题。 Thus I decided to answer here and give more details about this problem.因此,我决定在这里回答并提供有关此问题的更多详细信息。

This is now possible (with Poetry version 1.2; perhaps even an earlier version), using the "extras" group:现在可以使用“附加”组(使用 Poetry 版本 1.2;甚至可能是更早的版本):

poetry add redis --group=extras

It will appear in the section它将出现在部分

[tool.poetry.group.extras.dependencies]

which is also newer style (compared to [tool.poetry.extras] or [tool.poetry.extras.dependencies]这也是较新的风格(与[tool.poetry.extras][tool.poetry.extras.dependencies]

See the documentation .请参阅文档 Interestingely, this still follows the older style, [tool.poetry.extras] , and doesn't show the use of poetry add , but the above result is what I get.有趣的是,这仍然遵循旧样式[tool.poetry.extras] ,并且没有显示使用poetry add ,但上面的结果是我得到的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM