简体   繁体   English

从 `pyproject.toml` 生成 Python 解释器不容忍的轮子

[英]Generating Python interpreter-intolerant wheels from a `pyproject.toml`

Consider the following pyproject.toml :考虑以下pyproject.toml

[build-system]
requires = ["setuptools>=40.8.0", "wheel"]
[project]
name = "foo"
version = "0.0.0"
requires-python = "~=3.9"

If I run pip wheel.如果我运行pip wheel. in the directory containing this file, then I generate a wheel named foo-0.0.0-py3-none-any.whl .在包含此文件的目录中,然后我生成一个名为foo-0.0.0-py3-none-any.whl However, this wheel filename indicates that any python3 interpreter is fine, yet my requires-python metadata in my pyproject.toml indicates that only python3.9 is acceptable.然而,这个 wheel 文件名表明任何 python3 解释器都可以,但是我的pyproject.toml中的requires-python元数据表明只有 python3.9 是可以接受的。

How can I get the requires-python metadata to be reflected in the wheel?如何让requires-python元数据反映在轮子中? I would expect the wheel filename to be foo-0.0.0-cp39-cp39-any.whl in this case.在这种情况下,我希望 wheel 文件名是foo-0.0.0-cp39-cp39-any.whl . . . .

That is not quite what the platform tag in the wheel filename is used for - cp39 would indicate that you're only compatible with CPython 3.9 or higher, and this wheel should not be selected by PyPy or some other implementations.这不完全是 wheel 文件名中的平台标签的用途 - cp39 表示您只与CPython 3.9 或更高版本兼容,并且 PyPy 或其他一些实现不应选择此 wheel。 You would usually only use a compatibility tag like that if you had some compiled C extensions inside the wheel that are CPython-specific.如果您在 wheel 中有一些特定于 CPython 的已编译 C 扩展,您通常只会使用这样的兼容性标签。

The Requires-Python metadata is still located inside your built wheel, which you'll see if you try to install it on an incompatible Python version: Requires-Python元数据仍然位于您构建的 wheel 中,如果您尝试将它安装在不兼容的 Python 版本上,您将看到它:

$ python3.8 -m pip install ./foo-0.0.0-py3-none-any.whl
Processing ./foo-0.0.0-py3-none-any.whl
ERROR: Package 'foo' requires a different Python: 3.8.13 not in '~=3.9'

The location of the metadata is here:元数据的位置在这里:

$ unzip foo-0.0.0-py3-none-any.whl
Archive:  foo-0.0.0-py3-none-any.whl
  inflating: foo-0.0.0.dist-info/METADATA  
  inflating: foo-0.0.0.dist-info/WHEEL  
  inflating: foo-0.0.0.dist-info/top_level.txt  
  inflating: foo-0.0.0.dist-info/RECORD  
$ grep Requires foo-0.0.0.dist-info/METADATA
Requires-Python: ~=3.9

As for how this works with PyPI - the index may return this metadata in the json API ( example ) and in the simple API ( example ) .至于这如何与 PyPI 一起工作——索引可能会在json API示例)和简单的 API示例中返回此元数据。 That allows pip to avoid downloading and unpacking incompatible wheels.这允许 pip 避免下载和解压不兼容的轮子。

It's in a data-requires-python attribute of the href - you might have to "view-source" in your browser to see it. 它位于 href 的data-requires-python属性中——您可能必须在浏览器中“查看源代码”才能看到它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM