22 lines
845 B
Python
22 lines
845 B
Python
import os
|
|
import importlib.util
|
|
|
|
def run_components():
|
|
components_dir = "components"
|
|
for filename in os.listdir(components_dir):
|
|
if filename.endswith(".py") and filename != "__init__.py":
|
|
module_name = filename[:-3] # 去掉 .py 后缀
|
|
module_path = os.path.join(components_dir, filename)
|
|
|
|
spec = importlib.util.spec_from_file_location(module_name, module_path)
|
|
module = importlib.util.module_from_spec(spec)
|
|
spec.loader.exec_module(module)
|
|
|
|
if hasattr(module, "main") and callable(module.main):
|
|
print(f"正在调用 {module_name}.main()")
|
|
module.main()
|
|
else:
|
|
print(f"{module_name}.py 中没有找到 main 函数或 main 不是可调用对象。")
|
|
|
|
if __name__ == "__main__":
|
|
run_components() |