python要求_条件要求设置.py

我正在编写一个依赖于file-magic的library,它对大多数平台都很好,但是在Alpine Linux中,文件魔法不起作用,所以我需要使用python-magic库。在

现在我知道了如何编写自己的代码来处理不同的Python库api,但是我不知道如何编写setup.cfg或{}以根据我们正在进行安装的系统有不同的需求。在

我想最好的选择是使用PEP 508规则,但我不知道如何用那种语法说“libmagic like Alpine”或其他什么,更不用说在包的设置.py. 实际上,如果不安装file-magic并看着它死掉,我甚至不知道如何区分两种架构之间的区别:-(

当然,这类事情一定有最佳实践吗?在

更新

从下面的Tim那里得到了一些更广泛的理解之后,我拼凑出了这个方法,让它发挥作用:def get_requirements():

"""

Alpine is problematic in how it doesn't play nice with file-magic -- a

module that appears to be the standard for most other Linux distros. As a

work-around for this, we swap out file-magic for python-magic in the Alpine

case.

"""

config = configparser.ConfigParser()

config.read("setup.cfg")

requirements = config["options"]["install_requires"].split()

os_id = None

try:

with open("/etc/os-release") as f:

os_id = [_ for _ in f.readlines() if _.startswith("ID=")][0] \

.strip() \

.replace("ID=", "")

except (FileNotFoundError, OSError, IndexError):

pass

if os_id == "alpine":

requirements[1] = "python-magic>=0.4.15"

return requirements

setuptools.setup(install_requires=get_requirements())

这允许使用setup.cfg的声明性语法,但是如果安装目标是一个Alpine系统,则调整install_requires值。在

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值