Discover Plugin Metadata before Loading
Core idea
Installed distributions can advertise entry points with a group, name and target value. importlib.metadata.entry_points(group=...) selects declared candidates without calling their load method. Discovery is not authorization: review the distribution and target, reject duplicate names or define a policy, and load only trusted approved plugins.
from importlib.metadata import EntryPointep = EntryPoint(name='short', value='demo_tools:render', group='demo.renderers')print(ep.module, ep.attr)print(ep.group)Metadata can be inspected without importing demo_tools.
Select a group without loading
from importlib.metadata import EntryPoint, EntryPointspoints = EntryPoints([ EntryPoint('csv', 'demo:csv', 'demo.export'), EntryPoint('text', 'demo:text', 'demo.export'), EntryPoint('other', 'demo:other', 'demo.view'),])print(sorted(p.name for p in points.select(group='demo.export')))Selection filters declarations only. In an installed environment, entry_points(group=...) supplies the corresponding discovered candidates.
Common mistake
EntryPoint.load imports a target and can execute package initialization. Do not load unknown declarations just to display a list. Custom metadata providers may also run code during discovery.
Practice
Create two synthetic entries with the same group and name. Design a rejection policy without calling load.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Plugin discovery
Check your understanding of plugin discovery.
What is printed?
from importlib.metadata import EntryPointe=EntryPoint('x','pkg:run','demo')print(e.attr)What is printed?
from importlib.metadata import EntryPoint, EntryPointse=EntryPoints([EntryPoint('x','pkg:run','a')])print(len(e.select(group='b')))What does load do?
How should duplicate names be handled?