dynamic loading of modules

Skip Montanaro skip at pobox.com
Wed Mar 6 11:18:31 EST 2002


    Henry> I would like to be able to compound variables that will then form
    Henry> the name of the particular module to be loaded into the active
    Henry> script.

Take a look at the docs for the exec statement and the __import__ builtin
function.  Briefly:

    mname = raw_input("enter a module name: ")
    exec "import %s"%mname in globals()

or

    mname = raw_input("enter a module name: ")
    m = __import__(mname, globals())
    print m.__doc__

-- 
Skip Montanaro (skip at pobox.com - http://www.mojam.com/)




More information about the Python-list mailing list