setuptools“至少其中之一”依赖规范

发布于 2024-08-17 15:43:22 字数 439 浏览 8 评论 0原文

在某些情况下,有各种模块,每个模块都实现通用 API(在我的例子中,是旧的纯 python elementtreecElementTreelxml.etree 和内置 xml.etree)。我可以使用 ElementTree 编写模块来尝试每个选项,并根据我自己的偏好顺序采用第一个存在的选项 - 但我找不到任何有关指定仅其中一个选项的信息这些必须安装在 setup.py 中。我想写一些看起来像这样的东西:

setup(
    ...,
    install_requires="""
        elementtree | cElementTree | lxml
    """,
    ...
)

这个或类似的东西可能吗?

In some cases, there are various modules which each implement a common API (in my case, the old pure-python elementtree, cElementTree, lxml.etree, and the built-in xml.etree). I can write the module using ElementTree to try each of these options, and take the first one that exists according to my own preference order -- but I can't find any information on specifying that only one of these must be installed in setup.py. I want to write something that looks like this:

setup(
    ...,
    install_requires="""
        elementtree | cElementTree | lxml
    """,
    ...
)

Is this, or something like it, possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

痴情 2024-08-24 15:43:22

我不这么认为,但是,如果您使用的是相当新的 Python,elementtree 是标准 Python 库的一部分,为什么您担心它可能不存在呢? (我确实知道这对于 API 的几种可能实现的其他情况来说是一个问题,我只是想知道您是否真的需要它来满足您的特定用例)。

I don't think so, but, if you're using a reasonably recent Python, elementtree being part of the standard Python libraries, why do you worry it might be absent? (I do understand this would be a problem for other cases of several possible implementations of an API, I just wonder if you really need it for your specific use case).

油饼 2024-08-24 15:43:22

您可能想尝试在 setup.py 中执行类似的操作:

install_requires = ["elementree"]

try:
    import cElementTree
    install_requires = []
except ImportError:
    try:
        import lxml
        install_requires = []
    except ImportError:
        # etc for remaining equivalent modules

setup(
    install_requires = install_requires,
    # rest of setup parameters
)

如果未安装等效项,这基本上会将 elementree 包安装为依赖项。

You might want to try doing something like this in setup.py:

install_requires = ["elementree"]

try:
    import cElementTree
    install_requires = []
except ImportError:
    try:
        import lxml
        install_requires = []
    except ImportError:
        # etc for remaining equivalent modules

setup(
    install_requires = install_requires,
    # rest of setup parameters
)

This will basically install elementree package as dependency if none of the equivalent are installed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文