dynamic import - Dynamically importing Python module -


I have a reliable remote server that stores several custom python modules. I can get them through text via HTTP (eg urllib2.urlopen as text / field), but I can not save the faxed module code to the local hard disk. How can I import code as a fully operational python module, including its entire global variable and import?
I think I have to use some combination of the exec and imp modules function, but I am unable to work it yet.

It seems that should do the trick:

 >>> Import small Saw >>> foo = imp.new_module ("foo") >>> foo_code = "" ... square fu: ... pass ... "" >>> F foo_code foo .__ dict__ >>> Foo.Foo .__ module_ 'foo' >>> 

Also, as suggested in the ActiveState article, you can assign your new module to sys.modules :

 >>> Import Arrangement >>> sys.modules ["foo"] = foo >>> foo import from foo  foo ... >> >> >> 

Comments