special case loading modules with imp
Steven Taschuk
staschuk at telusplanet.net
Wed Jul 30 13:21:49 EDT 2003
Quoth Erendil at aol.com:
[...]
> I need my plugin module to use imp to load that string of code as a module.
[...]
> So, I just want to switch from using real files to being able to use strings.
> The goal is to get a module object that I can place in an array.
I was going to suggest StringIO, but it turns out imp.load_module
wants a real file, not merely something file-like.
However, you can always do it by hand:
>>> import imp
>>> sourcecode = 'def foo(x): return 11*x'
>>> mod = imp.new_module('foo')
>>> exec sourcecode in mod.__dict__
>>> mod.foo(16)
176
--
Steven Taschuk staschuk at telusplanet.net
"I tried to be pleasant and accommodating, but my head
began to hurt from his banality." -- _Seven_ (1996)
More information about the Python-list
mailing list