0

I'm trying to embed python into my C++ application, but also make it standalone, i.e. do not rely on python dependencies on a disk, like Libs folder with standard library scripts, but still be able to use them. I can find a way to store all the the scripts from Libs in the binary by myself and load them into the memory after running the program, but the problem is that I don't know how to "load" them into python interpreter directly from memory, either from text or binary (compiled) scripts, without dropping them on disk, how pyinstaller does (it extracts all compiled scripts into temp system dir).

I'm wondering if there is any kind of callback in cpython that allows a user to override import keyword and general modules import logic so I could import a module from memory instead of loading it from disk. I couldn't find anything like that on google. Maybe you know if there is such feature?

Thanks in advance!

g00dds
  • 195
  • 8
  • https://github.com/MarekR22/pycpp_example - did some time ago for other SO question (author has deleted this question). – Marek R Aug 05 '22 at 13:37
  • @MarekR, it seems like it's not what I want. I want to create a standalone executable like `pyinstaller` does, but without dropping files on disk. In your example it imports a module from disk then calls a function from it, I guess it won't run without preinstalled python because of missing critical files like `os.py`, `encodings/utf8.py`, etc. So I need to somehow make these files importable from memory – g00dds Aug 05 '22 at 13:55
  • 1
    Related: [How to load compiled python modules from memory?](https://stackoverflow.com/questions/1830727/how-to-load-compiled-python-modules-from-memory) and [PEP 302](https://peps.python.org/pep-0302/). – Scott McPeak Aug 06 '22 at 08:00

0 Answers0