Python有很多依赖项 [英] Python any of many dependencies

查看:94
本文介绍了Python有很多依赖项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不时遇到一种情况,即我有一个可以依赖于A或B软件包的软件包。

From time to time I come across a situation where I have a package that can depend on either package A or B.

例如,我的项目取决于a名为 spam 的软件包,如果将此项目重命名为 pyspam ,则我的项目可以依赖于垃圾邮件 pyspam

For example, my project depends on a package called spam, if this project is renamed to pyspam, my project can either depend on spam or pyspam.

我不知道(或找不到)我会怎么做在setup.py中定义此类依赖项。

I cannot figure out (or find) how I would define such dependencies in setup.py. What is a commonly accepted way to solve this?

编辑:我想在 setup.py 。像这样的东西:

I would like to define the dependencies in setup.py. Something like this:

from setuptools import setup

setup(
    name='myproject',
    install_requires=[
        'spam || pyspam'
    ]
)


推荐答案

您可以检查该软件包是否可用,并根据该软件包决定使用哪个软件包。 (我希望我能正确理解您的问题。)

You can check to see if the package is available, and decide which to use based on that. (I hope I understood your question correctly).

from setuptools import setup

imp_spam = "spam"
try:
    import spam
except:
    imp_spam = "pyspam"

setup(
    name='myproject',
    install_requires=[imp_spam]
)

这篇关于Python有很多依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆