Friday, 16 October 2020

Access list of imported modules from collect_submodules in hiddenimports from PyInstaller

I have an application bundled using PyInstaller. One of the hooks I created added some hiddenimports from a nested package: hiddenimports = collect_submodules("mypkg.subpkg"). In another part of my code, I am trying to import all of the modules that are in that mypkg.subpkg so that I can iterate over the classes in each one. I was able to do this successfully using:

modules = [module for module in globals()['__loader__'].toc if "mypkg.subpkg" in module]
for mod_name in modules:
    imported_module = importlib.import_module(mod_name)
    for item in dir(imported_module):
        value = getattr(imported_module, item)
        if not value or not inspect.isclass(value):
            continue

However, that globals()['__loader__'].toc is not very intuitive and not documented in PyInstaller's documentation (I found it through another stack overflow post and saw it in the source code). Is there another way to access the names of the modules that were imported using hiddenimports in the PyInstaller hook?

Note: I'd like to avoid using collect_data_files because I don't want the py files in the final bundle



from Access list of imported modules from collect_submodules in hiddenimports from PyInstaller

No comments:

Post a Comment