Dynamically importing .py files after compiling

290 Views Asked by At

I've tried looking online and I'm honestly lost at this point.

I'm trying to find if there's a way to import python scripts and run them AFTER my Python program has been compiled.

For an example, let's say I have a main.py such that:

import modules.NewModule

a = NewModuleClass()
a.showYouWork()

then I compile main.py with pyinstaller so that my directory looks like:

main.exe
modules/NewModule.py

My end goal is to make a program that can dynamically read new Python files in a folder (this will be coded in) and use them (the part I'm struggling with). I know it's possible, since that's how add-ons work in Blender 3D but I've struggled for many hours to figure this out. I think I'm just bad at choosing the correct terms in Google.

Maybe I just need to convert all of the Python files in the modules directory to .pyc files? Then, how would I use them?

Also, if this is a duplicate on here (it probably is), please let me know. I couldn't find this issue on this site either.

1

There are 1 best solutions below

1
On

You may find no detailed answer simply because there is no problem. PyInstaller does not really compile Python scripts into machine code executables. It just assembles then into a folder along with an embedded Python interpretor, or alternatively creates a compressed single file executable that will automatically uncompress itself at run time into a temporary folder containing that.

From then on, you have an almost standard Python environment, with normal .pyc file which can contain normal Python instructions like calls to importlib to dynamically load other Python modules. You have just to append the directory containing the modules to sys.path before importing them. An other possible caveat, is that pyinstaller only gets required modules and not a full Python installation, so you must either make sure that the dynamic modules do not rely on missing standard modules, or be prepared to face an ImportError.