12

Possible Duplicate:
How to load compiled python modules from memory?

I have some Python file in the memory that may be StringIO. How can I import module file stored in the memory? I do not want to save it to disk and then load.

It looks like:

import StringIO.StrngIO([buf]) 
BSMP
  • 4,596
  • 8
  • 33
  • 44
simon
  • 569
  • 9
  • 20
  • 2
    http://stackoverflow.com/questions/1830727/how-to-load-compiled-python-modules-from-memory – Himanshu Jan 07 '13 at 07:54
  • Regarding the recent edit: This Meta StackExchange post explains the duplicate notice being in the post itself: https://meta.stackexchange.com/questions/338534/duplicate-post-notice-missing-the-duplicate – BSMP May 21 '20 at 08:02

1 Answers1

15

A nice approach is to use custom Meta import hooks as described in PEP 302. One can write a class that imports modules dynamically from a dictionary of strings:

"""Use custom meta hook to import modules available as strings. 
Cp. PEP 302 http://www.python.org/dev/peps/pep-0302/#specification-part-2-registering-hooks"""
import sys
import imp

modules = {"a" : 
"""def hello():
    return 'Hello World A!'""",
"b":
"""def hello():
    return 'Hello World B!'"""}    

class StringImporter(object):

   def __init__(self, modules):
       self._modules = dict(modules)


   def find_module(self, fullname, path):
      if fullname in self._modules.keys():
         return self
      return None

   def load_module(self, fullname):
      if not fullname in self._modules.keys():
         raise ImportError(fullname)

      new_module = imp.new_module(fullname)
      exec self._modules[fullname] in new_module.__dict__
      return new_module


if __name__ == '__main__':
   sys.meta_path.append(StringImporter(modules))

   # Let's use our import hook
   from a import hello
   print hello()
   from b import hello
   print hello()

BTW: If you don't want that much and just want to import one string, then stick to the implementation of the method load_module. All you need is inside it.

Thorsten Kranz
  • 12,492
  • 2
  • 39
  • 56
  • 1
    how would this work with packages ? thanks – Cobry Aug 04 '18 at 11:32
  • Not sure... Packages are basically a collection of modules. Maybe playing around with the name during import might bring the illusion of importing packages. What is your use case? – Thorsten Kranz Aug 06 '18 at 06:25
  • @ThorstenKranz In Python 3, why do I get this `exec(self._modules[fullname] in new_module.__dict__) TypeError: exec() arg 1 must be a string, bytes or code object`? I made the required changes to Python 3 (enclosing print and exec inside parentheses) – ihavenoidea Nov 24 '20 at 04:18