import hooks (custom module loaders) for pypy do not work
Question: I'm successfully able to create import hooks to load files directly from
memory in python2.7. The example I used was the accepted response to this
question:

[python:Import module from
memory](http://stackoverflow.com/questions/14191900/pythonimport-module-from-
memory)

However; when applying this code on pypy; i get an import error. I have also
tried other import hook examples that work with regular python but not with
pypy, such as this:

[python load zip with modules from
memory](http://stackoverflow.com/questions/39135750/python-load-zip-with-
modules-from-memory/39136473#39136473)

Does anyone know why import hooks do not work in pypy? Is there something I am
missing?


Answer: The problem is that in both of the examples you point to, `load_module()` does
not add the loaded module to `sys.modules`. Normally, it should do so (and
then PyPy works like CPython).

If `load_module()` does not add the module to `sys.modules`, then every single
`import a` will call `load_module()` again and return a new copy of the
module. For example, in the example from [python:Import module from
memory](http://stackoverflow.com/questions/14191900/pythonimport-module-from-
memory):

    
    
    import a as a1
    import a as a2
    print a1 is a2   # False!
    a1.foo = "foo"
    print a2.foo     # AttributeError
    

This is documented in <https://www.python.org/dev/peps/pep-0302/#id27>. The
`load_module()` method is responsible for doing more checks than these simple
examples show. In particular, note this line (emphasis in the original):

> Note that the module object _must_ be in sys.modules before the loader
> executes the module code.

So, the fact that PyPy behaves differently than CPython in this case could be
understood as a behavior difference that follows from code that fails to
respect the docs.

But anyway, my opinion is that it should be fixed. I've created an issue at
<https://bitbucket.org/pypy/pypy/issues/2382/sysmeta_path-not-working-like-
cpythons>.

