Last active
January 11, 2016 04:16
-
-
Save tnewman/e236d8707f40944f3124 to your computer and use it in GitHub Desktop.
Python Plugin Loader
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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