What is the best way to extend a python package via optional packages? The idea is that you could so:
pip install mypackage
and optionally:
pip install mypackage-option1
Then you application will do:
from mypackage import somefunction
but somefunction
is aware of the existence of mypackage-option1
if it is installed.
I have seen a few ways of doing this via plugin systems, but I have a few requirements that I need to fulfill:
mypackage
should not explicitly use mypackage-option1
. (i.e. if in the future I have mypackage-option2
it should work without changing mypackage
)mypackage
should not need to import mypackage-option1
If your plugin packages' names will always start with mypackage-
(and with your no-configuration requirement you probably need a convention like that), you can do:
import pkgutil
plugin_names = [
name for (loader, name, ispkg) in pkgutil.iter_modules()
if name.startswith('mypackage-')]
That will get you the package names as strings. Then you can use __import__()
to dynamically import modules.