How to write a minimally working pyproject.toml file that can install packages?

孤街浪徒 提交于 2021-01-26 10:00:24

问题


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 it should not in PEP518 which only deals with build dependencies). 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-written pyproject.toml.


回答1:


All these files are "human-writable". I will give an answer here for setuptools (a section for poetry was added later on). But it is important to know that in this context setuptools is called the build backend, and there are multiple such backends available today, setuptools is just one of them. Other build backends include poetry, 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.

Since a few days (December 2020) there is a standard (PEP621) in which it is specified how a project's metadata (including dependencies) should be laid out in a pyproject.toml file. But as far as I know, no tool has adopted it yet, it is obviously too early for that. -- January 2021: pdm seems to have added support for the PEP 621 notation.


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:

  • https://python-poetry.org/docs/pyproject/
  • https://python-poetry.org/docs/dependency-specification/


来源:https://stackoverflow.com/questions/64150719/how-to-write-a-minimally-working-pyproject-toml-file-that-can-install-packages

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!