Search code examples
pythonpluginspackagingextending

Packages as plugins in Python


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:

  • Python 2.6+ and 3.2+.
  • Zero configuration.
  • mypackage should not explicitly use mypackage-option1. (i.e. if in the future I have mypackage-option2 it should work without changing mypackage)
  • The application using mypackage should not need to import mypackage-option1
  • No configuration files or list stored in the user directory or anywhere else.

Solution

  • 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.