python modules as user configuration files

Alex Martelli aleaxit at yahoo.com
Fri Apr 6 05:59:37 EDT 2001


"Robin Thomas" <robin.thomas at starmedia.net> wrote in message
news:mailman.986503148.3570.python-list at python.org...
    [snip]
> I would recommend not using "import", which would actually install the
> module in sys.modules and possibly screw up imports of other, legitimate
> modules you have created. Also, importing from a fullpath is not very
> straightforward.

I agree that execfile looks like a better match for the original
poster's needs (except it may need the ```exec "pass"''' kludge
to use it inside a function, as another thread just mentioned),
but "importing from a fullpath" isn't all that hard, I think.

Something like the following might suffice, I believe:

def importFullpath(path, modname):
    import imp

    try: found = imp.find_module(modname, [path])
    except: return None

    try: return imp.load_module(modname, *found)
    finally: file.close()


Alex






More information about the Python-list mailing list