How to execute lists of code? Or alternatives.

Fredrik Lundh effbot at
Tue May 9 11:39:45 EDT 2000

Remco Gerlich wrote:
> What I would love to have is to __import__ a module from a string.
> I don't think that's possible though.

# import module from string
# fredrik lundh, may 2000

import imp, sys

def do_import(name, source):
    module = imp.new_module(name)
    sys.modules[name] = module
    exec source in vars(module)
    return module

script = """
def hello():
    print 'hello you too'

do_import("mymodule", script)

import mymodule


<!-- (the eff-bot guide to) the standard python library:

More information about the Python-list mailing list