Science and technology

The best way to use trendy Python packaging and setuptools plugins collectively

Python packaging has developed rather a lot. The newest (“beta”) makes use of one file, pyproject.toml, to manage the package deal.

A minimal pyproject.toml would possibly appear to be this:

[build-system]
requires = ["setuptools"]
build-backend = "setuptools.build_meta"

[project]
identify = "cool_project"
model = "1.0.0"

The challenge part

The challenge part is the info in regards to the Python challenge itself, together with fields like identify and model, that are required.

Other fields are sometimes used, together with:

  • description: A one-line description.
  • readme: Location of a README file.
  • authors: Author names and e-mails.
  • dependencies: Other packages utilized by this challenge.

The build-system part

Though it doesn’t should be first, the build-system normally goes on the prime. This is as a result of it’s a very powerful one.

The build-backend key factors to a module that is aware of find out how to construct supply distributions and wheels from the challenge. The requires discipline permits specifying construct time dependencies.

Many initiatives are constructed with setuptools. There are some new alternate options, like flit and hatch.

Plugins

One advantage of the requires part in build-system is that it may be used to put in plugins. The setuptools package deal, specifically, can use plugins to switch its habits.

One factor that plugins can do is to set the model routinely. This is a well-liked want as a result of model administration can typically be painful.

Segue

Before persevering with, it’s value reflecting on the character of “parodies”. A parody of X is an occasion of X which exaggerates some points, typically to the purpose of humor.

For instance, a “parody of a spy movie” is a spy film, even because it riffs on the style.

A parody of setuptools plugins

With this in thoughts, what would a parody of a setuptools plugin appear to be? By the rule above, it needs to be a plugin.

The plugin, referred to as onedotoh, units the model to… 1.0.0. In order to be a plugin, it first needs to be a package deal.

A package deal ought to have a pyproject.toml:

[build-system]
requires = ["setuptools"]
build-backend = "setuptools.build_meta"

[project]
identify = "onedotoh"
model = "1.0.0"

[project.entry-points."setuptools.finalize_distribution_options"]
setuptools_scm = "onedotoh:guess_version"

There is a brand new part: challenge.entry-points. This signifies that the operate guess_version shall be referred to as as setuptools is able to finalize the distribution choices.

The code of guess_version is one line:

def guess_version(dist):
    dist.metadata.model = "1.0.0"

Version to 1.0.0

Using onedotoh is refined. One downside is writing the pyproject.toml challenge part to appear to be this:

[project]
identify = "a_pyproject"
model = "0.1.2"

The model within the pyproject.toml will override the model from the plugin.

The apparent answer is to take away the model discipline:

[project]
identify = "a_pyproject"

This fails differently. Without the model within the challenge part, the file is invalid. The identify won’t be used.

The proper option to do it’s as follows:

[project]
identify = "a_pyproject"
dynamic = ["version"]

This strategy explicitly declares the model discipline as dynamic.

A full instance will appear to be this:

[build-system]
requires = [
    "setuptools",
    "onedotoh",
]
build-backend = "setuptools.build_meta"

[project]
identify = "a_pyproject"
dynamic = ["version"]

Finally, the model is routinely set to 1.0.0.

Wrap up

The setuptools plugin can nonetheless be used with trendy Python packaging, so long as related options are explicitly declared as “dynamic.” This makes a discipline rife for additional experimentation with automation.

For instance, what if, along with guessing the model from exterior metadata, it will guess the identify as nicely, utilizing the git distant URL?

Most Popular

To Top