This recipe lets you import any file as a module. It's not required that the file is in the Python search path. This is done without having to modify sys.path (even temporarily)
1 2 3 4 5 6 7 8 | import imp, os.path
def import_(filename):
(path, name) = os.path.split(filename)
(name, ext) = os.path.splitext(name)
(file, filename, data) = imp.find_module(name, [path])
return imp.load_module(name, file, filename, data)
|
Of course the extension of the filename will be ignore, e.g. m = import_("/usr/local/lib/python2.2/mimetypes.foo") will work, and will import mimetypes.pyc and not the non-existing mimetypes.foo
Potential name conflicts in sys.modules. imp.load_modules will also put the imported module in sys.modules, using as a key the name determined in import_ (see below). If some other module was already there, it will be replaced. So to some extent you are loosing control of your program's state.
Customized module import is not easy to do in clean way.
Managing sys.modules with load_module. The important thing is to manage the names of your modules in a way that avoids namespace conflicts and always (re)loads a module in the same place in sys.modules. This recipe might work better like this:
This produces the following output:
It's still tricky -- nothing prevents someone from importing a different 'example_re,' and overwriting your entry in sys.modules, for instance. If you're not prepared to deal with this sort of thing, though, you shouldn't muck about with runtime module loading.