【问题标题】:Limit package to CPython - setup.cfg将包限制为 CPython - setup.cfg
【发布时间】:2021-04-26 06:29:07
【问题描述】:

我根据 PEP-517 使用 pyproject.tomlsetup.cfg

我的setup.cfg 如下:

[metadata]
name = Package
version = 1.0.0

[options]
py_modules = ...
python_requires = >=3

我希望将包限制为仅在 CPython 上运行,因为它解决了特定于实现的问题。

我尝试过使用环境标记,但不幸的是它们不适用于python_requires 字段:

python_requires = >=3; implementation_name == "cpython"

有什么方法可以在不使用已弃用的setup.py 的情况下实现我的愿望?

【问题讨论】:

  • 你可以尝试像install-requires=this-package-requires-cpython;implementation_name != "cpython"这样的hack
  • 是的,我想过使用那个 hack。虽然不喜欢。我想知道是否有真正的方法来做到这一点。
  • 如果你正在构建一个轮子,你可以在兼容性标签中限制实现,例如python setup.py bdist_wheel --python-tag=cp3 将生成一个 <pkg>-<ver>-cp3-none-any.whl,它只会与 CPython impl 一起安装。对于源分布,恐怕您将不得不在 setup.py 脚本中进行手动检查。
  • @hoefling 感谢您的提示,但为了使用bdist_wheel,我仍然需要setup.py(这是您命令行的一部分)。我尝试使用 pip wheel --build-option python-tag=cp3 . 运行构建选项,但 PEP 517 构建似乎不支持它。我想答案是暂时是不可能的。
  • 我的错,没有正确阅读您的问题。使用setup.cfg 更容易管理;只需添加带有python-tag=cp3 条目的[bdist_wheel] 部分。如果 setup.cfg 包含在源 dist 中并在 pip installing 时用于构建轮子,这也可能会解决 sdist 的情况;不确定,因为我不知道如何从 PEP 517 声明性配置构建 sdist。

标签: python setuptools python-packaging


【解决方案1】:

不幸的是,您不能通过环境标记来限制实现,并且包元数据没有定义任何合适的字段。但是,可以通过轮元数据中的语言实现标签来限制实现;标签的格式在PEP 425 中定义。 CPython 实现缩写为cp,您的配置中的python_requires = >=3 意味着完整标签的cp3。在setup.cfg[bdist_wheel] 部分设置标签:

[metadata]
...

[options]
...

[bdist_wheel]
python_tag=cp3

这对bdist_wheel来说并不特殊;任何distutils 子命令都可以在setup.cfg 中有一个部分,可以在其中保留其命令行选项。阅读Writing the Setup Configuration File了解更多详情。

回答 cmets 的问题:

说实话,我什至还没有找到python-tag 命令的在线文档。我错过了什么吗?

这确实有些隐蔽; wheel's docs 不提供bdist_wheel 选项的参考。通常,您通过python setup.py bdist_wheel --help 列出它们(就像任何其他子命令一样),但由于您没有安装脚本,您可以伪造一个:

$ python -c "from setuptools import setup; setup()" bdist_wheel --help
Options for 'bdist_wheel' command:
  ...
  --python-tag      Python implementation compatibility tag (default: 'py3')

构建的 wheel 文件现在将具有 cp3-none-any 后缀(以及 wheel 元数据中的 cp3-none-any 标记),并且将被其他实现拒绝:

$ pypy3 -m pip install meowpkg-0.0.1-cp3-none-any.whl 
ERROR: meowpkg-0.0.1-cp3-none-any.whl is not a supported wheel on this platform.

这也适用于源 dist,因为pip 将始终从符合 PEP 517 的 sdist 构建一个轮子(而不是诉诸旧版 setup.py install),因此这也可以安全地用于源 dist。示例:

$ python -m build
$ pip uninstall -y wheel  # for testing only
WARNING: Skipping wheel as it is not installed.
$ pip install dist/meowpkg-0.0.1.tar.gz 
Processing ./dist/meowpkg-0.0.1.tar.gz
  Installing build dependencies ... done
  Getting requirements to build wheel ... done
    Preparing wheel metadata ... done
Building wheels for collected packages: meowpkg
  Building wheel for meowpkg (PEP 517) ... done
  Created wheel for meowpkg: filename=meowpkg-0.0.1-cp3-none-any.whl size=1005 sha256=d87571afcdeda6be74a182b9e3e1ce6035a4aea4bb173c291900a85e53232983
  Stored in directory: /home/oleg.hoefling/.cache/pip/wheels/1a/8c/43/90d6b484adcf2515d25503b0c47f14565cadf0e891a597e23e
Successfully built meowpkg
Installing collected packages: meowpkg
  Attempting uninstall: meowpkg
    Found existing installation: meowpkg 0.0.1
    Uninstalling meowpkg-0.0.1:
      Successfully uninstalled meowpkg-0.0.1
Successfully installed meowpkg-0.0.1

【讨论】:

    猜你喜欢
    • 2023-02-10
    • 2021-12-09
    • 2018-09-24
    • 2011-03-14
    • 2020-06-11
    • 1970-01-01
    • 1970-01-01
    • 2022-09-27
    • 1970-01-01
    相关资源
    最近更新 更多