Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is pyproject.toml file for?

Background

I was about to try Python package downloaded from GitHub, and realized that it did not have a setup.py, so I could not install it with

pip install -e <folder>

Instead, the package had a pyproject.toml file which seems to have very similar entries as the setup.py usually has.

What I found

Googling lead me into PEP-518 and it gives some critique to setup.py in Rationale section. However, it does not clearly tell that usage of setup.py should be avoided, or that pyproject.toml would as such completely replace setup.py.

Questions

Is the pyproject.toml something that is used to replace setup.py? Or should a package come with both, a pyproject.toml and a setup.py?
How would one install a project with pyproject.toml in an editable state?

like image 427
np8 Avatar asked Jul 19 '20 17:07

np8


People also ask

What is the use of Pyproject toml?

What is pyproject. toml? The pyproject. toml file was introduced in PEP-518 (2016) as a way of separating configuration of the build system from a specific, optional library (setuptools) and also enabling setuptools to install itself without already being installed.

What is Python toml?

TOML—Tom's Obvious Minimal Language—is a reasonably new configuration file format that the Python community has embraced over the last couple of years. TOML plays an essential part in the Python ecosystem. Many of your favorite tools rely on TOML for configuration, and you'll use pyproject.

How do you update a Pyproject poem toml?

To update to the latest versions, use the update command. This will fetch the latest matching versions (according to your pyproject. toml file) and update the lock file with the new versions. (This is equivalent to deleting the poetry.

What is setup CFG used for?

setup. cfg is a file which might be used to specify such options in addition to reading the command line when calling python setup.py <somecommand> . The documentation for setup.


Video Answer


3 Answers

Yes, pyproject.toml is the specified file format of PEP 518 which contains the build system requirements of Python projects.

This solves the build-tool dependency chicken and egg problem, i.e. pip can read pyproject.toml and what version of setuptools or wheel one may need.

If you need a setup.py for an editable install, you could use a shim in setup.py:

#!/usr/bin/env python

import setuptools

if __name__ == "__main__":
    setuptools.setup()
like image 163
pce Avatar answered Oct 13 '22 13:10

pce


pyproject.toml is the new unified Python project settings file that replaces setup.py. Editable installs still need a setup.py: import setuptools; setuptools.setup()

To use pyproject.toml, run python -m pip install .

Then, if the project is using poetry instead of pip, you can install dependencies (into %USERPROFILE%\AppData\Local\pypoetry\Cache\virtualenvs) like this:

poetry install

And then run dependencies like pytest:

poetry run pytest tests/

And pre-commit (uses .pre-commit-config.yaml):

poetry run pre-commit install
poetry run pre-commit run --all-files
like image 34
Cees Timmerman Avatar answered Oct 13 '22 13:10

Cees Timmerman


What is it for?

Currently there are multiple packaging tools being popular in Python community and while setuptools still seems to be prevalent it's not a de facto standard anymore. This situation creates a number of hassles for both end users and developers:

  1. For setuptools-based packages installation from source / build of a distribution can fail if one doesn't have setuptools installed;
  2. pip doesn't support the installation of packages based on other packaging tools from source, so these tools had to generate a setup.py file to produce a compatible package. To build a distribution package one has to install the packaging tool first and then use tool-specific commands;
  3. If package author decides to change the packaging tool, workflows must be changed as well to use different tool-specific commands.

pyproject.toml is a new configuration file introduced by PEP 517 and PEP 518 to solve these problems:

... think of the (rough) steps required to produce a built artifact for a project:

  1. The source checkout of the project.
  2. Installation of the build system.
  3. Execute the build system.

This PEP [518] covers step #2. PEP 517 covers step #3 ...

Any tool can also extend this file with its own section (table) to accept tool-specific options, but it's up to them and not required.

PEP 621 suggests using pyproject.toml to specify package core metadata in static, tool-agnostic way. Which backends currently support this is shown in the following table:

enscons flit_core hatchling pdm-pep517 poetry-core setuptools
Supports
0.26.0+
Supports
3.2+
Supports
0.3+
Supports
0.3.0+
Doesn't support
Issue #3332
Supports
61.0.0+

Does it replace setup.py?

For setuptools-based packages pyproject.toml is not strictly meant to replace setup.py, but rather to ensure its correct execution if it's still needed. For other packaging tools – yes, it is:

Where the build-backend key exists, this takes precedence and the source tree follows the format and conventions of the specified backend (as such no setup.py is needed unless the backend requires it). Projects may still wish to include a setup.py for compatibility with tools that do not use this spec.

How to install a package in editable mode?

Originally "editable install" was a setuptools-specific feature and as such it was not supported by PEP 517. Later on PEP 660 extended this concept to packages using pyproject.toml.

There are two possible conditions for installing a package in editable mode using pip:

  • Modern:
    Both the frontend (pip) and a backend must support PEP 660.
    pip supports it since version 21.3;
  • Legacy:
    Packaging tool must provide a setup.py file which supports the develop command.
    Since version 21.1 pip can also install packages using only setup.cfg file in editable mode.

The following table describes the support of editable installs by various backends:

enscons flit_core hatchling pdm-pep517 poetry-core setuptools
Supports
0.28.0+
Supports
3.4+
Supports
0.3+
Supports
0.8.0+
Supports
1.0.8+
Supports
64.0.0+
like image 49
EvgenKo423 Avatar answered Oct 13 '22 14:10

EvgenKo423