Skip to content

Instantly share code, notes, and snippets.

@daeken
Created June 16, 2010 17:17
Show Gist options
  • Save daeken/440975 to your computer and use it in GitHub Desktop.
Save daeken/440975 to your computer and use it in GitHub Desktop.
import imp, marshal
def moduleFromData(name, data):
"""
Loads a module from a pyc string in memory.
`name` -- Name of the module
`data` -- Contains the contents of a pyc file in a string
Returns a module object
"""
code = marshal.loads(data[8:])
locals = {}
eval(code, {}, locals)
module = imp.new_module(name)
for k, v in locals.items():
setattr(module, k, v)
return module
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment