How to handle files imported using __import__ in Pyinstaller

Viewed 131

Here's an example of the file-structure of my app, which I'm trying to turn into a standalone distributable one-dir application using auto-py-to-exe:

- plugins/file1.py
- plugins/file2.py
- plugins/...
- plugins/display/file1.py
- plugins/display/...
- main.py
- UI.py

The files which are under the plugins directory are dynamically imported using __import__. Here's the part of the code that does that:

for plugin_filename in plugin_files[ plugin_path ]:
    plugin_file = plugin_filename.replace('.pye', '').replace('.py', '')
    plugin = __import__(plugin_file)

Of course, this doesn't get recognized by the Pyinstaller analysis and the plugin files don't get bundled into the exe. From the documentation I've gathered that I need to use the --hidden-import option, so I tried this but with no success. This results in ERROR: Hidden import not found: enter image description here

Is there any way I can add these files to the exe?

1 Answers

I’ve managed to solve this by adding the absolute path of the plugins folder to paths and adding file1, file2, etc. to the —-hidden-import option.

I also had to get rid of the if statement that checks if the file is a Python one and replaced that block with a loop that looks something like this:

modules = [‘file1’, ‘file2’]
for module in modules:
    plugin = __import__(module)
Related