Question about import hooks

E

Ed Schofield

Hi all,

I am the author of the ``future`` package for Python 2/3 compatibility (http://python-future.org). A bug report has recently been posted about its use of import hooks that I don't yet have an answer for, and I am looking for some guidance on how to customize the import mechanism in a safer way.

The current interface is as follows:

Any subsequent import statements using Python 3-style module names are mapped onto the relevant Python 2-style names (or, where needed, backported modules provided by ``future``). For example, these then work in the same way on both Python 2 and Python 3:

Although this is a nice interface, reminiscent of ``from __future__ import ...``, the problem is that the current implementation, which appends finder objects to the ``sys.meta_path`` list (http://docs.python.org/2/library/sys.html#sys.meta_path) renders the import hooks globally, even for modules imported by other modules. What I want instead is for the import hooks to apply only to a particular module, so that a script containing:

from future import standard_library
import requests

would not apply the import hooks to modules imported within the ``requests`` module, merely to import statements in the script itself.

There is a note in the Python 3.3 documentation (and the current Python 3.4 draft) that I had hoped would provide the answer for how to implement this:

"When calling __import__() (http://docs.python.org/3/library/functions.html#__import__)
as part of an import statement, the import system first checks the module global namespace for a function by that name. If it is not found, then the standard builtin __import__() (http://docs.python.org/3/library/functions.html#__import__)
is called."


If this were true, it would be possible to change the interface to something like this:

which would then override the ``__import__`` function in ``builtins`` or ``__builtin__`` affecting subsequent ``import`` statements only in that module. The interface wouldn't be quite as nice, but it wouldn't cause the import hooks to bleed into other modules that don't need them. However, the docs seem to be wrong; defining __import__ as a global seems to have no effect on imports in Py3.3, and ``future`` needs to implement this for Python 2 anyway.

Can you think of a way to implement import hooks safely, for one particular module, while providing a nice clean interface? Preferably this would remain accessible through a one-line import like ``from future import standard_library``.

Thanks in advance for any ideas!

Best wishes,
Ed
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,060
Latest member
BuyKetozenseACV

Latest Threads

Top