Import a module from a non-file?

Peter Hansen peter at engcorp.com
Fri Oct 17 11:37:47 EDT 2003


Petri Savolainen wrote:
> 
> I was trying to roll my own python code importer, but in the end, it seems
> that no matter what you try, it is always necessary to supply a REAL file
> object for python lower-level import machinery to work. Is this really
> true? Or did I miss something?
> 
> Of course, it is easy to get the module source from somewhere, put it into a
> tmpfile etc. ... but I'd be nice to be able to skip that extra step.

You don't say what you actually tried.  Doesn't imp.load_module() allow
passing a "file-like object" containing the source?

What about exec?  I would think that if you read in the source code string,
created a new module, and passed its dictionary in to exec, you would get
basically the same result.

(Neither idea is based on personal success doing this... they're just ideas.)

-Peter




More information about the Python-list mailing list