Setuptools 平台特定的依赖项 [英] Setuptools platform specific dependencies

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

问题描述

有没有办法告诉 setuptools 或分发在特定平台上需要一个包?

Is there any way to tell setuptools or distribute to require a package on a specific platform?

在我的特定情况下,我使用 readline,它是 Unix 系统标准库的一部分,但在 Windows 上我需要 pyreadline 模块来替换该功能(参见这个问题).如果我只是把它放在需求中它也会安装在完全没用的Unix系统上.

In my specific case, I'm using readline, which comes as part of the standard library on Unix systems, but on Windows I need the pyreadline module to replace that functionality (cf. this question). If I just put it in the requirements It also installs on Unix systems where it's completely useless.

推荐答案

当我第一次在这里写答案时,2013 年,我们还没有 PEP 496 – 环境标记PEP 508 – Python 软件包的依赖规范.现在我们这样做了,答案是:将环境标记放在您的 setup_requires 中:

When I first wrote my answer here, in 2013, we didn't yet have PEP 496 – Environment Markers and PEP 508 – Dependency specification for Python Software Packages. Now that we do, the answer is: put environment markers in your setup_requires:

setup_requires = [
    'foo',
    'bar',
    'pyreadline; sys_platform == "win32"',
]

setup(
    # ...
    setup_requires=setup_requires,
)

setuptools 20.6.8,于 2016 年 5 月发布(在 版本 20.5,但在中间版本中被短暂禁用).

This is supported as of setuptools 20.6.8, released in May 2016 (support was introduced in version 20.5 but was briefly disabled in intervening releases).

请注意,setuptools 会在执行时使用 easy_install 来安装这些需求,这在使用 pip 安装项目时很难配置.

Note that setuptools will use easy_install to install those requirements when it is being executed, which is hard to configure for when using pip to install the project.

最好使用 setuptools 来处理构建时的依赖关系,并按照 PEP 518 – 指定 Python 项目的最低构建系统要求.使用带有内置依赖项的 PEP 518 构建系统,意味着创建一个 pyproject.toml 文件,如下所示:

It may better to not use setuptools to handle build-time dependencies, and use a pyproject.toml file following the recommendations from PEP 518 – Specifying Minimum Build System Requirements for Python Projects. Using the PEP 518 build-system with built-time dependencies, means creating a pyproject.toml file that looks something like this:

[build-system]
requires = [
    "setuptools",
    "wheel",
    "foo",
    "bar",
    "pyreadline; sys_platform == "win32",
]

这是与 setup_requires 相同的列表,但添加了 setuptoolswheel.pip10.0.0 版,于 2018 年 3 月发布.

That's the same list as setup_requires but with setuptools and wheel added. This syntax is supported by pip as of version 10.0.0, released in March 2018.

我从 2013 年开始的旧答案如下.

My old answer, from 2013, follows.

setup.py 只是一个 python 脚本.您可以在该脚本中创建动态依赖项:

setup.py is simply a python script. You can create dynamic dependencies in that script:

import sys

setup_requires = ['foo', 'bar']

if sys.platform() == 'win32':
    setup_requires.append('pyreadline')

setup(
    # ...
    setup_requires=setup_requires,
)

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

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