Murray's Blog

Embedding Python and importing from memory

Time to ask the web again:

In Glom, I use PyRun_String() to execute python scripts from memory (the scripts are never on disk), and get the result. But I’d like those scripts to be able to import Python modules that are also in memory (in a virtual library of reusable code). Python’s import statement usually just looks for files in the Python import path. I’ve looked through the Python/C API reference, but I can’t see anything suitable. Unlike C, you can’t just paste the code into the start of the script, because being in a module affects the syntax.

I have a vague idea that PyImport_ExecCodeModule might make the Python code importable under the provided name, but it seems to require a compiled object, for which I need to supply a filename to PyNode_Compile().

I’d rather not write all the files to a temporary directory just so they can be imported. That seems like a fragile hack.


object = Py_CompileString(script_text, module_name, Py_File_Input) followed by PyImport_ExecCodeModule(module_name, object) seems to work. Thanks commenters. But now I wonder how to remove modules when they have been removed from the virtual library. Maybe I could do PyImport_Cleanup() each time, but that is documented as for internal use only.

Exit mobile version