简体   繁体   English

poetry 根据附加功能安装不同的 package 版本

[英]poetry install different package version based on extras

Using python-poetry, I would like to install different package versions based on the extras that I pass during the installation.使用 python-poetry,我想根据我在安装过程中传递的额外内容安装不同的 package 版本。 Eg I would like to例如我想

# when extra == 'a', install numpy == 1.20.0
$ poetry install -E a
# when extra == 'b', install numpy == 1.19.0
$ poetry install -E b

I tried it with the following toml file我尝试使用以下 toml 文件

[tool.poetry]
name = "demo-poetry"
version = "0.1.0"
description = ""
authors = ["tenticon"]

[tool.poetry.dependencies]
python = "^3.8"
numpy = [
    { version = "1.20.0", markers = "extra == 'a'", optional = true},
    { version = "1.19.0", markers = "extra == 'b'", optional = true}
]

[tool.poetry.extras]
a = [ "numpy" ]
b = [ "numpy" ]

[tool.poetry.dev-dependencies]
pytest = "^5.2"

[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"

but when I do $ poetry install -E a I get但是当我做$ poetry install -E a我得到

  SolverProblemError

  Because demo-poetry depends on both numpy (1.20.0) and numpy (1.19.0), version solving failed.

My poetry version is 1.1.6我的诗歌版本是1.1.6

I think there is some confusion between the concept of extras in poetry and pip.我认为诗歌中的临时演员的概念和 pip 之间存在一些混淆。 Poetry extras are sets of packages (eg math = ["numpy", "scipy"] ) that can be (optionally) installed together ( poetry install -E math ). Poetry extras是一组可以(可选)安装在一起的包(例如math = ["numpy", "scipy"] )( poetry install -E math )。 They are similar to the concept of groups which are introduced in the pre-release 1.2.0a2 .它们类似于在预发行版 1.2.0a2中引入的组的概念。 However, pip extras are related only to one package, ie they are dependencies that you can install in addition to the regular dependencies of a package, if you ask for them explicitly (eg pip install ludwig[text] ). However, pip extras are related only to one package, ie they are dependencies that you can install in addition to the regular dependencies of a package, if you ask for them explicitly (eg pip install ludwig[text] ).

It is indeed possible to use environment markers as install conditions for dependencies.确实可以使用环境标记作为依赖项的安装条件。 However, the extras defined in the PEP-0508 definition of environment markers relate to pip extras, and not poetry extras.但是, PEP-0508 环境标记定义中定义的附加内容与 pip 附加内容有关,而不是诗歌附加内容。 Thus, poetry extras can not be used as environment markers for constraining install conditions.因此,诗歌附加内容不能用作限制安装条件的环境标记。

Neither using extras nor groups (from the pre-release) was sufficient in order to achieve the expected results.使用附加组件或组(来自预发布)都不足以达到预期的结果。 In this regard, I think the comment from @finswimmer is correct: extras are not mutually exclusive, and poetry checks that the dependency resolution works in every case.在这方面,我认为@finswimmer的评论是正确的:附加项不是相互排斥的,并且诗歌检查依赖解决方案是否适用于每种情况。 The same happens with groups.团体也是如此。

The closest I could get to an acceptable solution was by defining conditions based on the python version, or the platform.我最接近可接受的解决方案是通过基于 python 版本或平台定义条件。 For instance, if you have the following pyproject.toml :例如,如果您有以下pyproject.toml

[tool.poetry]
name = "demo-poetry"
version = "0.1.0"
description = ""
authors = ["vreyespue"]

[tool.poetry.dependencies]
numpy = [
    { version = "1.19.0", python = "~3.7"},
    { version = "1.20.0", python = "~3.9"}
]

[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"

You could install different versions of numpy in different environments:您可以在不同的环境中安装不同版本的 numpy:

$ poetry env use 3.7
  Using virtualenv: /***/demo-poetry-***-py3.7

$ poetry install
  Installing numpy (1.19.0)

$ poetry env use 3.9
  Using virtualenv: /***/demo-poetry-***-py3.9

$ poetry install
  Installing numpy (1.20.0)

This bothered me for internal development, and while it was extremely frustrating since neither poetry nor cleo offer any documentation on their SDK, I finally managed to create a plugin for this ( pypi , git ).这困扰着我的内部开发,虽然这非常令人沮丧,因为poetrycleo都没有提供关于他们的 SDK 的任何文档,但我终于设法为此创建了一个插件( pypigit )。

The idea is that with this plugin, you can have non-mutual exclusive definitions in groups, and when using the --without or --only flags, the relevant groups are dropped from dependency parsing.这个想法是,使用这个插件,您可以在组中有非互斥定义,并且当使用--without--only标志时,相关组将从依赖项解析中删除。 The plugin needs to be installed prior to any such group definition.该插件需要在任何此类组定义之前安装。

Installation via poetry add poet-plugin (if starting from an empty, clean project, without such dependencies), or then via:通过poetry add poet-plugin安装(如果从一个空的、干净的项目开始,没有这样的依赖),或者通过:

poetry shell
pip install poet-plugin

I have not battle-tested this, but it works nicely for our use case with eg我还没有对此进行实战测试,但它非常适合我们的用例,例如

poetry install --only prod
poetry install --only dev
poetry install --without dev
poetry install --without prod

Feedback and updates etc are welcome, hope this momentarily resolves this issue for some of us out there.欢迎反馈和更新等,希望这能暂时为我们中的一些人解决这个问题。

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

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