1

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.

Higgsy
  • 324
  • 1
  • 14
  • Does this answer your question? [Importing external module in single-file exe created with PyInstaller](https://stackoverflow.com/questions/47350078/importing-external-module-in-single-file-exe-created-with-pyinstaller) – Kemp Dec 01 '20 at 10:09
  • Wow! Fast! I'm not sure. I'll try this out and get back to you. Hopefully it works well! Actually heading to bed now. I didn't expect to have a response this quickly. – Higgsy Dec 01 '20 at 10:18
  • do you mean like programs use dll files – an inconspicuous semicolon Dec 01 '20 at 10:23

1 Answers1

0

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.

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
  • There are caveats that I discuss in the answer here: https://stackoverflow.com/questions/47350078/importing-external-module-in-single-file-exe-created-with-pyinstaller – Kemp Dec 01 '20 at 13:10