Skip to content

Instantly share code, notes, and snippets.

@tnewman
Last active January 11, 2016 04:16
Show Gist options
  • Save tnewman/e236d8707f40944f3124 to your computer and use it in GitHub Desktop.
Save tnewman/e236d8707f40944f3124 to your computer and use it in GitHub Desktop.
Python Plugin Loader
import importlib
import pkgutil
import os
# Uses the package as the directory to search for plugins
plugin_directory=os.path.dirname(__file__)
# Loads all of the modules in this package
for module in pkgutil.iter_modules([plugin_directory]):
name_index=1
importlib.import_module('.' + module[name_index], __package__)
# Now you can do work by using baseclass.__subclasses__()
# to retrieve all of the classes that implement the plugin
# contract without needing to have a nasty list of imports
# just to add a new plugin. Imagine the possibilities for
# allowing end-users to add new plugins themselves.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment