Dynamic list of modules in a package

29 views
Skip to first unread message

Wesley Rodriguez Kronmiller

unread,
Aug 8, 2024, 9:32:15 PM8/8/24
to PyInstaller
For the program I'm writing, I set up a package which defines an interface for algorithms to convert CSV files from one format to another, scans the package directory for modules which implement the interface, then gives them to the importing program as a list[ModuleType], so that the importing program can list them in a dropdown and present them to the user. Here's the code I used to make the list of modules:

./conversionAlgos/__init__.py:
>>> algos: list[ModuleType] = sorted(
...     [
...         importlib.import_module("." + name, __name__)
...         for _, name, _ in pkgutil.iter_modules(__path__)
...     ],
...     key=lambda item: item.PRIORITY,
... )

In my main.py that I package with pyinstaller I access the modules like this:

./main.py
>>> from conversionAlgos import algos
... 
... for algo in algos:
...     print(algo.Converter().friendly_name)

algorithm 1
algorithm 2
etc.

This works fine when I run the program a script, but because the package isn't laid out in the filesystem after I build it with pyinstaller, it can't find the modules. Is there a better way to make the list of modules that will be compatible with pyinstaller?

bwoodsend

unread,
Aug 9, 2024, 6:52:42 AM8/9/24
to PyInstaller

pkgutil.iter_modules() doesn’t require Python files on the file system. The problem is that PyInstaller doesn’t know to collect these submodules at all. Use --collect-submodules=conversionAlgos to fix that although you almost certainly will run into #6294 so you’ll have to use one of the workarounds from there.

Wesley Rodriguez Kronmiller

unread,
Aug 12, 2024, 4:56:04 PM8/12/24
to PyInstaller
I couldn't get any of the workarounds in #6294 to work, `collect_submodules("conversionAlgos")` returned an empty list even if the package directory was in sys.path, but running `collect_submodules` in a hook worked. Thanks!
Reply all
Reply to author
Forward
0 new messages