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, at runtime I am trying to dynamically 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 at runtime 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