fork(1) download
  1. # your code goes here
  2. import imp
  3. modulesource = 'a=1;b=2' #load from internet or wherever
  4. def makemodule(modulesource,sourcestr='http://some/url/or/whatever',modname=None):
  5. if not modname: modname = 'newmodulename'
  6. #must be exec mode
  7. codeobj = compile(modulesource, sourcestr, 'exec')
  8. newmodule = imp.new_module(modname)
  9. exec(codeobj,newmodule.__dict__)
  10. return newmodule
  11. newmodule = makemodule(modulesource)
  12. print(newmodule.a)
  13.  
  14. modulesource = '''
  15. a = 'foo'
  16. def myfun(astr):
  17. return a + astr
  18. '''
  19. newmod = makemodule(modulesource)
  20. print(newmod.myfun('bat'))
Success #stdin #stdout 0.01s 7860KB
stdin
Standard input is empty
stdout
1
foobat