Skip to content

Use build.util.project_wheel_metadata to get the package dependencies/requirements #42

Open
@wwuck

Description

@wwuck

It looks like there is finally a standard supported way to get the package dependencies/requirements via PEP517.

pypa/build#181

From the end of the discussion on that issue, something like this should work:

import tempfile
from importlib.metadata import PathDistribution
from pathlib import Path

import pep517.wrappers
from build import ProjectBuilder
from packaging.requirements import Requirement

# replace "." with the package directory
builder = ProjectBuilder(".", runner=pep517.wrappers.quiet_subprocess_runner)

with tempfile.TemporaryDirectory() as tmpdir:
    distribution = PathDistribution(Path(builder.metadata_path(tmpdir)))
    requires = [Requirement(i) for i in distribution.requires]
    for x in requires:
        print(type(x))
        print(x)

This should work for any package/project that uses a PEP517 compatible build backend, eg. setuptools, flit, poetry, whey, etc. I've only tested it myself on https://github.com/pypa/sampleproject which uses setuptools.

EDIT: https://pypa-build.readthedocs.io/en/latest/api.html#build.util.project_wheel_metadata is now available and should be sufficient for this feature.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions