Skip to content

entry_point with un-satisfied extras_require doesn't throw a DistributionNotFound if you installed your package as a wheel #882

@jimporter

Description

@jimporter

This is somewhat-complex, but I think I have a handle on what's going on. I have a project with a setup.py that looks like this:

setup(
    extras_require={
        'msbuild': ['lxml'],
    },

    entry_points={
        'bfg9000.backends': [
            'msbuild=bfg9000.backends.msbuild.writer [msbuild]',
        ],
    },
)

The goal of this code is to say "the MSBuild backend requires the lxml package". Later, I do the following:

from pkg_resources import iter_entry_points, DistributionNotFound
# ...
for i in iter_entry_points('bfg9000.backends'):
    try:
        backend = i.load()
        backends.append((i.name, backend))
    except DistributionNotFound:
        pass

The above should load all the backends whose requirements are met (i.e. don't load the MSBuild backend if you don't have lxml). This works fine when the package was not installed as a Wheel, but if it's a Wheel, setuptools tries to actually import the MSBuild backend and raises an ImportError instead of DistributionNotFound.

It appears to be caused by this code, which from my reading says, "If the extras for this requirement are not satisfied, skip this requirement." However, the requirement in this case is lxml; extra == "msbuild" (in a non-Wheel installation, it's just lxml), so pkg_resources is basically saying, "It's ok if you don't have lxml; just try to load the MSBuild backend." Unfortunately, that's the opposite of what I want, and isn't consistent with the docs:

[An entry_point] can also include a bracketed list of “extras” that are required for the entry point to be used. When the invoking application or framework requests loading of an entry point, any requirements implied by the associated extras will be passed to pkg_resources.require(), so that an appropriate error message can be displayed if the needed package(s) are missing.

I assume "appropriate error message" in the above is supposed to mean a DistributionNotFound exception.

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