简体   繁体   English

在 Python 的 Pip 中,如何搜索与版本模式匹配的 package 的所有可能版本?

[英]In Python's Pip, how can you search for all possible versions of a package, which match a version pattern?

I'm trying to build a support matrix of requirements in my project.我正在尝试在我的项目中建立一个需求支持矩阵。 I use the ~= syntax to match minor versions, however because I'm packaging a PiPy client to corporate clients I really need to test all possible versions.我使用~=语法来匹配次要版本,但是因为我将 PiPy 客户端打包到企业客户端,所以我真的需要测试所有可能的版本。

I know how to search for all versions of a PiPy package using the hack pip install xyz== , but I really want to know how to search for all compatible versions given a requirement string.我知道如何使用 hack pip install xyz==搜索 PiPy package 的所有版本,但我真的很想知道如何在给定要求字符串的情况下搜索所有兼容版本。

ie what versions could possibly be installed (or won't conflict) for即可能会安装哪些版本(或不会冲突)

pip install xyz<=10

Or或者

pip install xyz~=9.0.0

(Pip install rules are quite complex) (pip 安装规则相当复杂)

While writing this question, I came up with this solution.在写这个问题时,我想出了这个解决方案。 Unfortunately it uses the internal Pip code, and I'm sure it can be can be condensed down, but it works with pip version 19.不幸的是,它使用了内部 Pip 代码,我确信它可以被压缩,但它适用于 pip 版本 19。

from pip._internal.models.format_control import FormatControl
from pip._internal.download import PipSession
from pip._internal.index import PackageFinder
from pip._internal.req import InstallRequirement
from pip._vendor.packaging.requirements import Requirement


package_finder = PackageFinder(
    find_links=[],
    format_control=FormatControl(set(), set()),
    index_urls=['https://pypi.org/simple'],
    trusted_hosts=[],
    allow_all_prereleases=False,
    session=PipSession(),
)


def get_valid_versions(requirement_string, include_preleases=False):
    install_req= InstallRequirement(
        Requirement(requirement_string), comes_from=None
    )
    all_candidates = package_finder.find_all_candidates(install_req.name)
    return set(
        install_req.specifier.filter(
            [str(c.version) for c in all_candidates],
            prereleases=include_preleases,
        )
    )

Usage:用法:

 >>> get_valid_versions('dask~=2.8.0')
 {'2.8.0', '2.8.1'}

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

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