1

I have this problem and do not know how to solve it efficiently.

I've this file structure

THE NUMBER OF FOLDERS NOR THE NAMES ARE GIVEN, IT'S ALL UNKNOWN

app/
    __init__.py
    modules/
        __init__.py
        ModuleA/
                __init__.py
                file.py
                otherfile.py
                config.ini
        ModuleB/
                __init__.py
                file.py
                otherfile.py
                config.ini
        ModuleC/
                __init__.py
                file.py
                otherfile.py
                config.ini

**arbitrary number of modules with same structure*

As you can notiche, app is the main package of my app, but I need an efficient way to import the mods folder and its' content

* My actual solution *

from app import modules ad mods

def load_modules_from_packages(self, pkg = mods):

        pkgname = pkg.__name__
        pkgpath = dirname(pkg.__file__)
        for loader,name,ispkg in pkgutil.walk_packages(pkg.__path__, pkgname+'.'):
            if ispkg is True:
                __import__(name,globals(),locals(),[],0)
            elif ispkg is False:
                __import__(name,globals(),locals(),[],0)

This works since pkgutil iterate the structure with the dot notation for names, so import works well.

But now I want load infos in the config file if I am in one of the somemodule folder(the one with own init.py and config.ini

I want to do this to recreate the structure of module package and output it in a JSON rapresentation for another thing

* my other solution does not works*

def load_modules_from_packages(directory)
    dir_path = dirname(directory.__file__)
    dir_name = directory.__name__
    for filename in glob.glob(dir_path + '/**/*.ini', recursive=True):
            plugin = {}
            plugin['name'] = filename.split('/')[-2]
            plugin['path'] = dirname(filename)
            plugin['config_file'] = filename
            for pyname in glob.glob(dirname(filename)+ '/**/*.py', recursive=True):
                importlib.import_module(pyname)

I cant use the solution posted in this thread How to import a module given the full path? since I do not know the module name, ad pointed without solutions in the comment.

  spec = importlib.util.spec_from_file_location('what.ever', 'foo.py')
  module = importlib.util.module_from_spec(spec)
  spec.loader.exec_module(module)

I know 'foo.py' but I cant figure out 'what.ever' like pkgutil.walk_package does.

In fact the modules imported in this way have the package and name entry wrong. With this approach I cant figure out where I am in the file structure to create modules dictionary and the relative modules (for the JSON output)

Any help?

Community
  • 1
  • 1
Mario
  • 131
  • 1
  • 5

0 Answers0