“tool.poetry” 和 “project” 在 pyproject.toml 中有什么区别?

huangapple go评论47阅读模式
英文:

What's difference between [tool.poetry] and [project] in pyproject.toml?

问题

  1. 这两者之间的主要区别是[project][tool.poetry]节的标题。

  2. 在你的pyproject.toml中应该只保留一个,通常是[tool.poetry]。以下是一个示例pyproject.toml文件:

[tool.poetry]
name = "example_package_YOUR_USERNAME_HERE"
version = "0.0.1"
authors = [
  { name = "Example Author", email = "author@example.com" }
]
description = "A small example package"
  1. 是的,如果只保留[tool.poetry],则需要按照[project]的规则来设置内容和子节,例如[project.urls]应该改为[tool.poetry.urls]

  2. 未来发布到PyPI的最佳选择是使用[tool.poetry],因为它是poetry工具的配置部分,而[project]是更旧的配置方式。

  3. 更改[build-system]poetry-coresetuptools通常是一个好主意,因为setuptools是Python包的常用构建工具,更为常见和成熟。

英文:

Context

So, I'm trying to create a new Python package following this tutorial: https://packaging.python.org/en/latest/tutorials/packaging-projects/

As the tutorial says, in my pyproject.toml I should have this structure:

[project]
name = "example_package_YOUR_USERNAME_HERE"
version = "0.0.1"
authors = [
  { name="Example Author", email="author@example.com" },
]
description = "A small example package"

but when I created this file with poetry init, it created this structure:

[tool.poetry]
name = "example_package_YOUR_USERNAME_HERE"
version = "0.0.1"
authors = [
  { name="Example Author", email="author@example.com" },
]
description = "A small example package"

The main difference between these two is the [project] instead of [tool.poetry] section header.
I also see, that poetry can't do anything with the project, when there is no [tool.poetry] section in pyproject.toml


So my questions are:

  1. What are the differences between these two?

  2. Should I have only one or both at the same time in my pyproject.toml? And in case I should keep both, what should it contain?

  3. In case there should be only [tool.poetry], do I need to follow the same rules for the content and sub-sections as for [project]? So for example [project.urls] would be renamed to [tool.poetry.urls]?

  4. What is the best future-proof choice for publishing on PyPI? Or are there no difference?

  5. Is changing the [build-system] from poetry-core to setuptools a good idea? Or I should keep poetry-core?

答案1

得分: 24

  1. 这两者之间有什么区别?
    [project] 部分已标准化(也被称为 PEP-621)。但 Poetry 比这个标准创建要早,所以它最初使用自己的部分 [tool.poetry]Poetry 计划添加对标准化 [project] 的支持(请参见 python-poetry/poetry/issues/3332python-poetry/roadmap/issues/3),但这需要时间。

这两者之间的区别非常小,基本上是相同软件包元数据的不同表示法。最显著的区别(您应该关注的区别)是关于声明依赖关系的表示法。这是分歧最关键的地方。

  1. 我的 pyproject.toml 中应该只有一个还是两者都需要同时存在?如果应该同时存在,应该包含什么内容?
    您应该只有一个。您需要选择一个构建后端。如果您的构建后端是 poetry-core,那么您需要 [tool.poetry] 部分。如果您选择需要 [project] 的构建后端(例如 setuptools),那么您应该使用它。

  2. 如果只应该有 [tool.poetry],我需要遵循与 [project] 相同的内容和子部分规则吗?例如 [project.urls] 是否应该更名为 [tool.poetry.urls]
    这不是一对一的等同,存在一些差异。如果您使用 Poetry,请参考 Poetry 的文档。或者如果您使用其他东西(如 setuptools 等),请参考 [project] 规范

  3. 对于发布到 PyPI,什么是最具未来性的选择?还是没有区别?
    这由您(和您的团队)决定。有人可能会主张选择遵循 [project] 标准的构建后端更具未来性,但这只是众多标准之一。以下表格比较了在做出这种选择时可以考虑的一些特点:

  1. [build-system]poetry-core 更改为 setuptools 是一个好主意吗?还是应该保留 poetry-core
    Poetry这个"开发工作流工具"不允许使用除 poetry-core 之外的任何其他构建后端。因此,如果您希望继续使用 Poetry 进行项目开发,那么只能保留 poetry-core 作为构建后端。
英文:

1. What are the differences between these two?

The [project] section is standardized (also known as PEP-621). But Poetry is older than the creation of this standard, so it started by using its own section [tool.poetry]. Poetry is planning to add support for the standardized [project] (see python-poetry/poetry/issues/3332 and python-poetry/roadmap/issues/3), but it takes time.

The differences between the two are quite small, they are basically different notations for the same package metadata. The most notable difference (the one you should keep an eye on) is regarding the notation for declaring the dependencies. This is where the divergence is the most critical.

2. Should I have only one or both at the same time in my pyproject.toml? And in case I should keep both, what should it contain?

You should have only one. You have to choose a build back-end. If your build back-end is poetry-core then you need the [tool.poetry] section. If you choose a build back-end that requires [project] (which is the case of setuptools), then that is what you should have.

3. In case there should be only [tool.poetry], do I need to follow the same rules for the content and sub-sections as for [project]? So for example [project.urls] would be renamed to [tool.poetry.urls]?

This is not exactly one-to-one equivalent, there are some differences. Follow Poetry's documentation if you use Poetry. Or the [project] specification if you use something else (setuptools, etc.).

4. What is the best future-proof choice for publishing on PyPI? Or are there no difference?

This is for you (and your team) to decide. One could argue that choosing a build back-end that follows the [project] standard is more future-proof, but it is only one criteria amongst many. The following tables compare some features one could take into account when making such a choice:

5. Is changing the [build-system] from poetry-core to setuptools a good idea? Or I should keep poetry-core?

Poetry the "development workflow tool" does not allow using any other build back-end than poetry-core. So if you want to keep using Poetry for your project, you have no choice but to keep using poetry-core as build back-end.

答案2

得分: 1

[project] 部分在 pyproject.toml 中是强制的。如果缺少该条目,则构建工具(在 [build-system] 部分定义)必须动态添加它。我想这正是 poetry 所做的事情。

来自文档

在此规范中定义的键必须在名为 [project] 的表中。没有工具可以向此表添加未由此规范定义的键。对于希望在 pyproject.toml 中存储自己设置的工具,可以使用构建依赖声明规范中定义的 [tool] 表。缺少 [project] 表意味着构建后端将动态提供所有键。

所以在使用 poetry 时,您不需要 [project]。如果更改构建系统,您必须将 pyproject.toml 转换为符合 PEP 621 的规范。

英文:

The [project] section is mandatory in pyproject.toml. If the entry is missing, the build tool (defined in [build-system] section) have to add it dynamically. I guess that's exactly what poetry does.

From the documentation:

> The keys defined in this specification MUST be in a table named [project] in pyproject.toml. No tools may add keys to this table which are not defined by this specification. For tools wishing to store their own settings in pyproject.toml, they may use the [tool] table as defined in the build dependency declaration specification. The lack of a [project] table implicitly means the build back-end will dynamically provide all keys.

So you don't need the [project] while you are using poetry. If you change the build system, you must convert your pyproject.toml to be PEP 621 compliant.

huangapple
  • 本文由 发表于 2023年2月10日 16:35:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/75408641.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定