Import module stored in a cStringIO data structure vs. physical disk file
Question: 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

[1] <http://docs.python.org/library/imp.html#imp.load_module>


Answer: Something like this perhaps?

    
    
    import types
    import sys
    
    src = """
    def hello(who):
        print 'hello', who
    """
    
    def module_from_text(modulename, src):
        if modulename in sys.modules:
            module = sys.modules[modulename]
        else:
            module = sys.modules[modulename] = types.ModuleType(modulename)
        exec compile(src, '<no-file>', 'exec') in module.__dict__
        return module
    
    module_from_text('flup', src)
    import flup
    flup.hello('world')
    

Which prints:

    
    
    hello world
    

**EDIT** :

Evaluating code in this way treads nearish the realm of writing custom
importers. It may be useful to look at [PEP
302](http://www.python.org/dev/peps/pep-0302/), and Doug Hellmann's [PyMOTW:
Modules and Imports](http://www.doughellmann.com/PyMOTW/sys/imports.html).

