importing text code
Remco Gerlich
scarblac at pino.selwerd.nl
Tue Feb 6 05:00:13 EST 2001
Dan Parisien <dan at eevolved.com> wrote in comp.lang.python:
> Is it possible to import from a string in memory instead of a file? I can't
> find information on how to do it...
I asked that last year and the effbot replied thusly:
# 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
mymodule.hello()
--
Remco Gerlich
More information about the Python-list
mailing list