Neat Trick for Loading Modules with unknown Name
Roger Binns
rogerb at rogerbinns.com
Mon Mar 22 13:04:17 EST 2004
> __import__ doesn't handle packages, right? The above with exec()
> appears, unfortunately, to be the simplest approach(*) to handling
> *arbitrary* imports, even if they are nested as in xml.sax.whatever.
Depends on your definition of *arbitrary* :-) I had the need to
load files that could have any arbitrary filename in any arbitrary
location (ie not even remotely anywhere near sys.path).
import imp
module=imp.load_source("__ignore", file, open(file, "rt"))
You can name your module something better than I did. I was loading
a whole bunch of files whose names are not even valid for Python
modules, and I didn't care what they thought they were called
(the module object above was stuck in a dictionary).
Roger
More information about the Python-list
mailing list