Is there a way to import a Python module stored in a cStringIO data structure vs. physical disk file?
It looks like “imp.load_compiled(name, pathname[, file])” is what I need, but the description of this method (and similar methods) has the following disclaimer:
Quote: “The file argument is the byte-compiled code file, open for reading in binary mode, from the beginning. It must currently be a real file object, not a user-defined class emulating a file.” [1]
I tried using a cStringIO object vs. a real file object, but the help documentation is correct – only a real file object can be used.
Any ideas on why these modules would impose such a restriction or is this just an historical artifact?
Are there any techniques I can use to avoid this physical file requirement?
Thanks,
Malcolm
Something like this perhaps?
Which prints:
EDIT:
Evaluating code in this way treads nearish the realm of writing custom importers. It may be useful to look at PEP 302, and Doug Hellmann’s PyMOTW: Modules and Imports.