1 year ago

#338559

test-img

mluerig

How to dynamically add entire plugin packages to main package namespace via entry points?

Consider this package that is intended for a main package my_package:

plugin-a-repo
├── setup.py
└── plugin_a
     ├── __init__.py 
     └── code.py

I made it discoverable by adding this to setup() in setup.py:

entry_points={'my_package.plugins':['plugin_a = plugin_a']}

Now I can load it from anywhere using

from importlib_metadata import entry_points
discovered_plugins = entry_points(group='my_package.plugins')
loaded_plugin_a = discovered_plugins["plugin_a "].load()

Question: How can I add this and other plugin packages to the main packages namespace, so that users, after importing my_package, can discover and access all available plugins through the main package's namespace:

dir(main_pkg.plugins) 
> ...
> plugin_a

or

from main_pkg.plugins.plugin_a.code import stuff 

I get the dynamic loading part (e.g. shown here https://stackoverflow.com/a/37285457/5238559), but I fail to see how I can add the loaded packages to the main package's namespace.

python

plugins

namespaces

entry-point

0 Answers

Your Answer

Accepted video resources