Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Python: Implement basic plugin architecture with Python and importlib

Implementing a basic plugin architecture shouldn't be a complicated task. The solution described here is working but you still have to import every plugin (inheriting from the base class).

This is my solution:

Basic project structure

$ tree
└── plugins

The base plugin

$ cat plugins/

import os
import traceback
from importlib import util

class Base:
    """Basic resource class. Concrete resources will inherit from this one
    plugins = []

    # For every class that inherits from the current,
    # the class name will be added to plugins
    def __init_subclass__(cls, **kwargs):

# Small utility to automatically load modules
def load_module(path):
    name = os.path.split(path)[-1]
    spec = util.spec_from_file_location(name, path)
    module = util.module_from_spec(spec)
    return module

# Get current path
path = os.path.abspath(__file__)
dirpath = os.path.dirname(path)

for fname in os.listdir(dirpath):
    # Load only "real modules"
    if not fname.startswith('.') and \
       not fname.startswith('__') and fname.endswith('.py'):
            load_module(os.path.join(dirpath, fname))
        except Exception:

A sample plugin

$ cat plugins/
import plugins

class PluginA(plugins.Base):

    def __init__(self):

    def start(self):
        print("Plugin A")

How to use it

$ cat
from plugins import Base

if __name__ == '__main__':
    for p in Base.plugins:
        inst = p()

Sample run

$ python
Plugin B
Plugin A
Copy link

charle-sh commented Jan 23, 2022

This is absolutely fantastic. I've spent the last few hours reading various guides on Python plugin architecture, but this is by far the most straightforward, uncomplicated code I've seen that implements automatic, dynamic loading at runtime. Thanks for putting this up, really helped me in my own project.

Copy link

dorneanu commented Jan 24, 2022

I'm glad this helped somehow. However, also make sure you check out/read Eli's post on more fundamental concepts of plugin infrastructures. He has some really interesting thoughts there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment