如何编写一个可以安装包的最小工作 pyproject.toml 文件? [英] How to write a minimally working pyproject.toml file that can install packages?
问题描述
Pip 支持 pyproject.toml
文件,但到目前为止,新模式的所有实际使用都需要一个自动生成这些文件的 3rd 方工具(例如,诗歌和 pip).与已经可以人工编写的 setup.py
不同,pyproject.toml
还不是.
来自 setuptools 文档,
[构建系统]需要 = [设置工具> = 40.9.0",轮子",]build-backend = "setuptools.build_meta";
但是,此文件不包含包依赖项(如 PEP 621 中所述).Pip 确实支持使用 pyproject.toml
安装包,但 pep 没有指定如何在 pyproject.toml
中为官方构建系统 setuptools
编写包依赖项.
如何在 pyproject.toml
中编写包依赖项?
相关的 StackOverflow 问题:
此问题要求一种自动生成
pyproject.toml
的方法,我的问题有所不同,因为我要求的是人工编写的pyproject.toml
.
PEP 621
2020 年 12 月
有一个标准叫做PEP621 指定项目的元数据(包括依赖项)应如何在 pyproject.toml
文件中布局.
2021 年 1 月
2021 年 5 月
更多构建后端添加了对PEP 621的支持,例如:
<块引用>
我的问题不同,因为我要求人工编写的 pyproject.toml
pyproject.toml
文件是人类可写"的;(以及 setup.cfg
).我将在此处为 setuptools 和 诗歌.
但首先,重要的是要知道在这种情况下,setuptools 和 poetry 表现为所谓的构建后端,并且目前有多个这样的后端可用,setuptools 只是其中之一.其他构建后端包括 flit、pymsbuild, pdm 等等.他们中的一些人希望他们的配置(包括依赖项)写在 pyproject.toml
中,有些人希望它在另一个文件中.
设置工具
截至今天(2020 年 10 月),setuptools 不支持在 pyproject.toml
中编写其配置.您仍然需要编写 setup.py
或 setup.cfg
,或两者的组合.
我的建议是在setup 中尽可能多地编写.cfg
,setup.py
可以很短:
import setuptools设置工具.setup()
这样的 setup.cfg
可能如下所示:
[元数据]名称 = 东西版本 = 1.2.3[选项]install_requires =SomeLibrary ~= 2.2包 = 查找:
关于依赖项的具体参考:
- https://setuptools.readthedocs.io/en/latest/userguide/dependency_management.html
- https://www.python.org/dev/peps/pep-0508/
- https://www.python.org/dev/peps/pep-0440/
另外,请注意在某些情况下可以完全省略 setup.py
文件,条件之一是 setup.cfg
文件和pyproject.toml
文件存在并包含所有必要的信息.下面是一个 pyproject.toml
示例,它适用于 setuptools 构建后端:
[构建系统]构建后端 = 'setuptools.build_meta'需要 = ['设置工具> = 43.0.0',]
最后,setuptools 有计划维护者,允许在 pyproject.toml
(而不是 setup.cfg
或 setup.py
)中编写配置,但我们还没有(2020 年 10 月).
诗歌
在诗歌中,一切都在pyproject.toml
中定义.
这个文件可以手写.据我所知,没有严格要求明确安装诗歌本身(诸如 pip install
和 pip wheel
之类的命令可以让您走得更远).>
pyproject.toml
文件可以很简单:
[tool.poetry]名称 = '东西'版本 = '1.2.3'[tool.poetry.dependencies]蟒蛇 = '^3.6'SomeLibrary = '~2.2'[构建系统]要求 = ['诗歌核心~=1.0']构建后端 = 'poetry.core.masonry.api'
参考文献:
- https://python-poetry.org/docs/pyproject/立>
- https://python-poetry.org/docs/dependency-specification/一个>
Pip supports the pyproject.toml
file but so far all practical usage of the new schema requires a 3rd party tool that auto-generates these files (e.g., poetry and pip). Unlike setup.py
which is already human-writeable, pyproject.toml
is not (yet).
From setuptools docs,
[build-system]
requires = [
"setuptools >= 40.9.0",
"wheel",
]
build-backend = "setuptools.build_meta"
However, this file does not include package dependencies (as outlined in PEP 621). Pip does support installing packages using pyproject.toml
but nowhere does pep specify how to write package dependencies in pyproject.toml
for the official build system setuptools
.
How do I write package dependencies in pyproject.toml
?
Related StackOverflow Questions:
How to init the pyproject.toml file
This question asks for a method to auto-generate
pyproject.toml
, my question differ because I ask for a human-writtenpyproject.toml
.
PEP 621
December 2020
There is a standard called PEP621 that specifies how a project's metadata, including dependencies, should be laid out in the pyproject.toml
file.
January 2021
pdm seems to have added support for the PEP 621 notation.
May 2021
More build back-ends have added support for PEP 621, for example:
my question differ because I ask for a human-written
pyproject.toml
The pyproject.toml
file is "human-writable" (as well as setup.cfg
). I will give an answer here for setuptools and for poetry.
But first, it is important to know that in this context setuptools and poetry behave as what are called build back-ends, and there are multiple such back-ends available today, setuptools is just one of them. Other build back-ends include flit, pymsbuild, pdm, and more. Some of them expect their configuration (including dependencies) to be written in pyproject.toml
, some expect it in another file.
setuptools
As of today (October 2020), setuptools does not support writing its configuration in pyproject.toml
. You still have to either write a setup.py
, or a setup.cfg
, or a combination of both.
My recommendation is to write as much as possible in setup.cfg
, and the setup.py
can be as short as:
import setuptools
setuptools.setup()
Such a setup.cfg
could look like this:
[metadata]
name = Thing
version = 1.2.3
[options]
install_requires =
SomeLibrary ~= 2.2
packages = find:
References about the dependencies specifically:
- https://setuptools.readthedocs.io/en/latest/userguide/dependency_management.html
- https://www.python.org/dev/peps/pep-0508/
- https://www.python.org/dev/peps/pep-0440/
As an aside, note that in some cases it is possible to omit the setup.py
file entirely, one of the conditions is that the setup.cfg
file and a pyproject.toml
file are present and contain all the necessary information. Here is an example of pyproject.toml
that works well for a setuptools build backend:
[build-system]
build-backend = 'setuptools.build_meta'
requires = [
'setuptools >= 43.0.0',
]
Finally, there are plans from the setuptools maintainers, to allow writing the configuration in pyproject.toml
(instead of setup.cfg
or setup.py
), but we are not there yet (October 2020).
poetry
In poetry everything is defined in pyproject.toml
.
This file can be hand-written. As far as I can tell, there is no strict need to ever explicitly install poetry itself (commands such as pip install
and pip wheel
can get you far enough).
The pyproject.toml
file can be as simple as:
[tool.poetry]
name = 'Thing'
version = '1.2.3'
[tool.poetry.dependencies]
python = '^3.6'
SomeLibrary = '~2.2'
[build-system]
requires = ['poetry-core~=1.0']
build-backend = 'poetry.core.masonry.api'
References:
这篇关于如何编写一个可以安装包的最小工作 pyproject.toml 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!