admin管理员组

文章数量:1124669

So i have a project in the following format.

main.py
src/plugins/a.py
src/plugins/b.py
src/plugins/n.py 
etc..

The modules in src/plugins/ are imported to main.py with a import for each module. I want to be able to make life a lot easier and just use a generic import such as from src.plugin.* import * instead of from src.plugin.A import A

Each file in src/plugins/ has the same format

imports


class A():

    def __init__(self, config):
        self.config = config
        self.plugin = 'A'


    def some_function_1(self):
        #do something

    def some_function_2(self):
        #do something

I know its bad practice to use a generic import but i currently find myself daily having to add imports so would prefer a generic import that will just do it automatically without me having to manually define it.

How would i be able to loop through the plugin modules if import using from src.plugins import *

for i in plugins:
    print(i.plugin)

So i have a project in the following format.

main.py
src/plugins/a.py
src/plugins/b.py
src/plugins/n.py 
etc..

The modules in src/plugins/ are imported to main.py with a import for each module. I want to be able to make life a lot easier and just use a generic import such as from src.plugin.* import * instead of from src.plugin.A import A

Each file in src/plugins/ has the same format

imports


class A():

    def __init__(self, config):
        self.config = config
        self.plugin = 'A'


    def some_function_1(self):
        #do something

    def some_function_2(self):
        #do something

I know its bad practice to use a generic import but i currently find myself daily having to add imports so would prefer a generic import that will just do it automatically without me having to manually define it.

How would i be able to loop through the plugin modules if import using from src.plugins import *

for i in plugins:
    print(i.plugin)
Share Improve this question edited 2 days ago Swannie asked 2 days ago SwannieSwannie 1351 silver badge10 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

Okay lets assume directory structure like so. You can use Python's importlib and pkgutil modules for you case.

project/
├── main.py
├── src/
│   ├── __init__.py
│   ├── plugins/
│   │   ├── __init__.py
│   │   ├── a.py
│   │   ├── b.py
│   │   ├── c.py

Dynamically import all modules in the plugin directory and load the like so

import importlib
import pkgutil
import inspect
from pathlib import Path

def load_plugins(plugin_dir):
    
    plugins = {}
    package = plugin_dir.replace("/", ".")
    for _, module_name, _ in pkgutil.iter_modules([plugin_dir]):
        module_path = f"{package}.{module_name}"
        module = importlib.import_module(module_path)
        plugins[module_name] = module
    return plugins


plugin_dir = Path(__file__).parent / "src/plugins"
loaded_plugins = load_plugins(str(plugin_dir))

in src/plugins/__init__.py: You can make this module-aware by adding:

from . import a, b, c

Let's say for src/plugins/a.py:

class A:
    def __init__(self, config):
        self.config = config
        self.plugin = "A"

when you run main.py you get

Loaded plugin: a
  Found class: A
  Instantiated A
Loaded plugin: b
  Found class: B
  Instantiated B

本文标签: python generic import to import multiple modulesStack Overflow