How to import a module based on an argument

Michel Claveau/Hamster No.Spam.mc at No.Spam.mclaveau.No.Spam.com
Fri Mar 26 21:18:04 CET 2004


Hi !

Have a good night, with this other solution :


# derived from the standard doc on Python
import imp,sys

def importAs(name):
    try:
        return sys.modules[name]
    except KeyError:
        pass
    fp, pathname, description = imp.find_module(name)
    try:
        return imp.load_module(name, fp, pathname, description)
    finally:
        if fp:
            fp.close()

mtext='string'
toto=importAs(mtext)
print toto.upper('sean berry')    # ==> SEAN BERRY




@-salutations
--
Michel Claveau
mél : http://cerbermail.com/?6J1TthIa8B
site : http://mclaveau.com











More information about the Python-list mailing list