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
1 Answer
Reset to default 1Okay 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
版权声明:本文标题:python generic import to import multiple modules - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736644351a1946071.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论