Plux Dynamic Code Loading Framework

1.16.0 · active · verified Sat Apr 11

Plux is a dynamic code loading framework for building pluggable Python distributions. It facilitates the discovery and loading of plugins defined via Python entry points, streamlining the process of creating extensible applications. The current version is 1.16.0, and the library maintains an active release cadence with several updates per year.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `plux.manager.PluginManager` to discover plugins. `plux` relies on Python entry points, typically defined in your project's `pyproject.toml` (or `setup.py`). For the example code to discover actual plugins, you need to define an entry point (e.g., under `[project.entry-points."my.plugins"]`) and ensure your project is installed in a way that makes its entry points discoverable (e.g., `pip install -e .`). The provided code will run without error, but will only report discovered plugins if they are set up externally.

import sys
from plux.manager import PluginManager

# --- In a real project, you would define your plugin entry point in pyproject.toml ---
# For example, in 'my_project/pyproject.toml':
#
# [project.entry-points."my.plugins"]
# my_example_plugin = "my_project.plugins:MyExamplePlugin"
#
# And have a 'my_project/plugins.py' file:
# class MyExamplePlugin:
#     def run(self):
#         return "MyExamplePlugin is running!"
#
# Then install your project in editable mode: `pip install -e .`
#
# --- This quickstart assumes such a plugin is discoverable ---

# Instantiate the PluginManager for a specific namespace
# Replace 'my.plugins' with your actual entry point group name
manager = PluginManager("my.plugins")

print(f"Searching for plugins in namespace 'my.plugins'...")
found_plugins = list(manager.discover())

if not found_plugins:
    print("No plugins discovered. Ensure a plugin is defined via entry points and installed.")
    print("Refer to the comments in this quickstart for typical setup steps.")
else:
    print(f"Discovered {len(found_plugins)} plugin(s):")
    for name, plugin_class in found_plugins:
        print(f" - Name: {name}, Class: {plugin_class.__name__}")
        try:
            plugin_instance = plugin_class()
            if hasattr(plugin_instance, 'run'):
                result = plugin_instance.run()
                print(f"   Instance result: {result}")
        except Exception as e:
            print(f"   Could not instantiate or run plugin {name}: {e}")

view raw JSON →