import vs imp and friends.

Emanuele D'Arrigo manu3d at gmail.com
Thu Jul 23 10:44:50 EDT 2009


Greetings,

I was looking in the archive of this newsgroup and I found this
snippet:

import imp
sourcecode = 'def foo(x): return 11*x'
mod = imp.new_module('foo')
exec sourcecode in mod.__dict__
mod.foo(16)

Together with similar and sometimes more complete snippets available
they show how a module can be created out of string, plain text files
and compiled files. Neat!

Now the question. Apart from checking sys.module first and eventually
adding the new module to it if it isn't there already, and apart from
setting __file__, is there anything else that import does and this
snippet doesn't?

Manu



More information about the Python-list mailing list