【问题标题】:How do I add a Python tag to the bdist_wheel command using setuptools?如何使用 setuptools 将 Python 标记添加到 bdist_wheel 命令?
【发布时间】:2019-03-07 16:12:07
【问题描述】:

假设我有一个简单的库,它使用setuptools 进行打包和分发。在这种情况下,库还需要最低版本的 Python 3.6,这意味着我的 setup.py 将如下所示:

from setuptools import setup, find_packages

setup(
    name='something',
    version='0.0.1',

    description='description',
    long_description=long_description,

    # More metadata

    packages=find_packages(exclude=['tests', 'docs']),

    python_requires='>=3.6'
)

现在,当我运行 python setup.py bdist_wheel 时,我得到一个名为 something-0.0.1-py3-none-any.whl 的文件。在这里很明显,当为我的轮子确定 Python 标记时,轮子忽略了setuptools 中的python_requires 选项(它应该是py36,但默认是py3)。显然,我意识到我可以从命令行传入--python-tag py36,这将完成这项工作,但我用于部署我的库的持续部署服务只接受我正在使用的发行版的名称(@987654330 @)。因此,我无法传递任何命令行参数。

经过一番研究,我发现我可以继承bdist_wheel类并覆盖python_tag成员变量,但是根据wheel README:

需要注意的是,wheel并非打算用作库,因此没有稳定的公共 API。

因此,我想避免从 bdist_wheel 类继承,这可能会迫使我在每次发生重大更改时重写我的类。

是否有任何通过 setuptools 的替代方法可以让我为轮子传递 Python 标记?

【问题讨论】:

    标签: python setuptools continuous-deployment python-wheel


    【解决方案1】:

    每个distutils 命令的每个命令行参数都可以保存在安装配置文件中。在您的setup.py 所在的同一目录中创建一个名为setup.cfg 的文件,并将自定义bdist_wheel 配置存储在其中:

    # setup.cfg
    [bdist_wheel]
    python-tag=py36
    

    现在运行python setup.py bdist_wheel 与运行python setup.py bdist_wheel --python-tag py36 基本相同。

    distutils 文档中的相关文章:Writing the Setup Configuration File

    【讨论】:

    【解决方案2】:

    你可以破解类似的东西

    if 'bdist_wheel' in sys.argv:
        if not any(arg.startswith('--python-tag') for arg in sys.argv):
            sys.argv.extend(['--python-tag', 'py36'])
    

    但它可以说是一样脆弱...

    【讨论】:

    • 我认为这在兼容性方面并不脆弱(因为 cli 参数 is 是公共 api)。但是,如果代码运行类似python setup.py bdist_wheel sdist 这样的解决方案,这个解决方案会起作用吗?
    猜你喜欢
    • 2013-07-22
    • 2017-10-04
    • 2021-09-29
    • 1970-01-01
    • 2021-01-12
    • 2022-01-17
    • 1970-01-01
    • 2020-01-18
    • 2012-03-04
    相关资源
    最近更新 更多