Skip to content

[ENHANCEMENT] Using importlib.metadata for plugin discovery #1699

Description

@David-Baddeley

Moving from conda to pip packaging has broken our plugin installation route (plugins drop files in the distribution /etc folder). Running into this tying to bring ch-shrinkwrap builds up to date (python-microscopy/ch-shrinkwrap#29).

It sounds like the best way to fix it would be to use importlib.metadata (see AI generated info below)


The canonical resource is the Python Packaging User Guide's plugin guide: https://packaging.python.org/en/latest/guides/creating-and-discovering-plugins/ — the "Using Package Metadata" section covers exactly this pattern.

And the stdlib docs: https://docs.python.org/3/library/importlib.metadata.html#entry-points

How it would work for PYME:

The plugin package (ch-shrinkwrap) declares entry points in pyproject.toml:

[project.entry-points."pyme.plugins.recipes"]
ch_shrinkwrap = "ch_shrinkwrap.recipe_modules.surface_fitting"
ch_shrinkwrap_features = "ch_shrinkwrap.recipe_modules.surface_feature_extraction"

[project.entry-points."pyme.plugins.visgui"]
ch_shrinkwrap = "ch_shrinkwrap.visgui_modules.shrinkwrap"

PYME's startup code replaces the .txt file scanning with:

from importlib.metadata import entry_points

for ep in entry_points(group='pyme.plugins.recipes'):
    ep.load()  # imports the module, triggering any registration side-effects

The metadata lives in the wheel's dist-info/ directory and is available immediately after pip install — no user action, no file copying, works identically for editable and non-editable installs.

Precedent to study: pytest's pytest11 group is the most widely known example of this pattern. Sphinx uses sphinx.builders, sphinx.directives etc. Both are worth looking at for how they handle errors when a plugin fails to load.

Backward compat note: entry_points(group=...) keyword syntax requires Python 3.9+ or importlib_metadata backport. For older Python, the older API is entry_points().get('pyme.plugins.recipes', []).

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

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions