Discover package contributions¶
Register an entry point when a separately installed package should contribute an object to your application. The application selects a named group and queries the resulting objects by type. Use an environment with Lookups installed.
Register a contribution¶
Choose a group owned by the consuming application, such as acme_reports.renderers. Use that same name in the contributing package and in EntryPointLookup. The Python packaging specification recommends starting custom group names with a PyPI name owned by the consumer, followed by a dot.
Create this package layout in a directory named acme-report-contribution:
acme-report-contribution/
├── pyproject.toml
└── src/
└── acme_report_contribution/
└── __init__.py
Put the following in pyproject.toml. The quoted table name registers plain in the acme_reports.renderers group, with the value identifying a callable in the package.
[build-system]
requires = ["hatchling >= 1.26"]
build-backend = "hatchling.build"
[project]
name = "acme-report-contribution"
version = "0.1.0"
requires-python = ">=3.11"
[project.entry-points."acme_reports.renderers"]
plain = "acme_report_contribution:create_renderer"
Define the contributed object and its zero-argument factory in src/acme_report_contribution/__init__.py:
class Renderer:
def render(self, text: str) -> str:
return f"Report: {text}"
def create_renderer() -> Renderer:
return Renderer()
The target may also be a class whose constructor takes no arguments. In either case, it must return a non-None member. If several packages implement a shared role, define its searched type in a common package so consumers and contributions import the same type.
For other build backends or existing metadata, see the packaging guide's entry-point configuration.
Install and query the contribution¶
From the parent of acme-report-contribution, install it using the interpreter that runs the application:
python -m pip install ./acme-report-contribution
Installation supplies the metadata that discovery reads. Merely placing the source directory on the import path does not register the entry point.
Save this consumer as discover.py outside the package directory, then run python discover.py:
from acme_report_contribution import Renderer
from lookups import EntryPointLookup
renderers = EntryPointLookup("acme_reports.renderers")
renderer = renderers.lookup_expected(Renderer)
print(renderer.render("Quarterly totals"))
Expected output:
Report: Quarterly totals
The query searches for Renderer. The name plain identifies the contribution
in package metadata and is not a lookup query key. Use lookup_all(Renderer) when the application needs every matching renderer, without relying on a particular contribution order.
Construction runs contribution code
EntryPointLookup immediately loads and calls every target in its group, before the first query. A failed import or factory prevents the lookup from being constructed, with
the exception identifying the contribution to investigate.
Membership stays fixed after construction. After installing or changing contributions, restart the application and construct a new lookup. For objects that should be created only when queried, use deferred conversion. Entry-point discovery constructs its members immediately.
Contribute to the application default¶
Keep a custom group when the application should choose where to expose contributions, perhaps by combining lookups. Use lookup.default, with singular lookup, when contributions should participate in Lookup.get_default().
For a controlled check, use an isolated environment with Lookups installed and no other packages contributing to lookup.default. In the example package's pyproject.toml, replace the entry-point table with:
[project.entry-points."lookup.default"]
plain = "acme_report_contribution:create_renderer"
Reinstall the package with python -m pip install ./acme-report-contribution, then save this as default_renderer.py. Run it in a fresh process with python default_renderer.py:
from acme_report_contribution import Renderer
from lookups import Lookup, ProxyLookup
default = Lookup.get_default()
assert isinstance(default, ProxyLookup)
assert Lookup.get_default() is default
renderer = default.lookup_expected(Renderer)
print(renderer.render("Default totals"))
This prints Report: Default totals. With only this ordinary object contributed, default resolution caches a ProxyLookup over the entry-point source. A contributed Lookup or LookupProvider changes resolution: a Lookup takes precedence, and its contents determine what the default exposes. See default lookup resolution before contributing either type.
Initial default resolution is cached, and there is no public reset API. Restart the process after changing installed default contributions. Do not rely on a winner among several competing default lookups or providers.
Diagnose discovery failures¶
If a query finds no renderer, inspect the installed metadata with the same interpreter:
python -m pip show acme-report-contribution
python -c "from importlib.metadata import entry_points; print(entry_points(group='acme_reports.renderers'))"
For the default example, substitute lookup.default in the second command. If the package is missing, install it in this interpreter's environment. If the group is empty, check the table name and reinstall after editing metadata. A missing or empty group produces an empty lookup. A required query then raises
NoneLookupError if no member matches.
If construction raises instead, follow the traceback to the target import or call. Check that the module is installed, the target exists and is callable without arguments, and its factory returns an object. A target must return a member, because None raises
ValueError('None cannot be a lookup member'). Exceptions from the factory
propagate to the caller.
For further checks, see Diagnose lookup problems.