问题描述
我根据 PEP-517 使用 pyproject.toml
和 setup.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
的情况下实现我的愿望?
解决方法
不幸的是,您不能通过环境标记来限制实现,并且包元数据没有定义任何合适的字段。但是,可以通过轮元数据中的语言实现标签来限制实现;标签的格式在 PEP 425 中定义。 CPython 实现缩写为 cp
,配置中的 python_requires = >=3
意味着 cp3
表示完整标记。在 [bdist_wheel]
的 setup.cfg
部分设置标签:
[metadata]
...
[options]
...
[bdist_wheel]
python_tag=cp3
这对bdist_wheel
来说并不特殊;任何 distutils
子命令都可以在 setup.cfg
中有一个部分,可以在其中保留其命令行选项。通读Writing the Setup Configuration File了解更多详情。
从评论中回答您的问题:
老实说,我什至没有找到 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')
构建的轮文件现在将具有 cp3-none-any
后缀(以及轮元数据中的 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.
这也适用于 source dist,因为 pip
将始终从符合 PEP 517 的 sdists 构建轮子(而不是求助于旧版 setup.py install
),因此这对于 source 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