2

PEP 302——新的导入挂钩指定了挂钩 Python 导入机制的方法。其中之一是创建一个模块查找器/加载器并将其添加到sys.meta_path.

我寻求创建一个能够重新路由子包导入的模块查找器。所以假设我写import mypackage.sub它应该实际导入模块mypackage_sub

mypackage.sub                => mypackage_sub
mypackage.sub.another        => mypackage_sub.another
mypackage.sub.another.yikes  => mypackage_sub.another.yikes

我尝试在不成功的地方实现这样的模块查找器。现在,我将查找器类安装在 Python 主文件中,但我的目标是将它安装在根包mypackage中。

finder 将收到与 的电话.find_module(fullname, path)'mypackage'但永远不会与'mypackage.sub'。因此,我只收到以下代码的以下错误。

Traceback (most recent call last):  
  File "test.py", line 63, in <module>  
    import mypackage.sub  
ImportError: No module named sub
import sys
sys.meta_path.append(_ExtensionFinder('mypackage'))
import mypackage.sub

这是该_ExtensionFinder课程的代码。我感谢任何允许我将此类_ExtensionFinder放入根模块的解决方案mypackage

class _ExtensionFinder(object):
    """
    This class implements finding extension modules being imported by
    a prefix. The dots in the prefix are converted to underscores and
    will then be imported.

    .. seealso:: PEP 302 -- New Import Hooks
    """

    def __init__(self, prefix):
        super(_ExtensionFinder, self).__init__()
        self.prefix = prefix

    def _transform_name(self, fullname):
        if self.prefix == fullname:
            return fullname
        elif fullname.startswith(self.prefix + '.'):
            newpart = fullname[len(self.prefix) + 1:]
            newname = self.prefix.replace('.', '_') + '_' + newpart
            return newname
        return None

    def find_module(self, fullname, path=None):
        print "> find_module({0!r}, {1!r})".format(fullname, path)

        newname = self._transform_name(fullname)
        if newname:
            return self

    def load_module(self, fullname):
        print "> load_module({0!r})".format(fullname)

        # If there is an existing module object named 'fullname'
        # in sys.modules, the loader must use that existing module.
        if fullname in sys.modules:
            return sys.modules[fullname]

        newname = self._transform_name(fullname)
        if newname in sys.modules:
            sys.modules[fullname] = sys.modules[newname]
            return sys.modules[newname]

        # Find and load the module.
        data = imp.find_module(newname)
        mod = imp.load_module(newname, *data)

        # The __loader__ attribute must be set to the loader object.
        mod.__loader__ = self

        # The returned module must have a __name__, __file__
        # and __package__ attribute.
        assert all(hasattr(mod, n) for n in ['__name__', '__file__', '__package__'])

        return mod

编辑:我发现描述我想要实现的正确词语是“命名空间包”

4

1 回答 1

0

显然,__path__必须设置属性,否则导入机制将不会调用sys.meta_path. 所以当模块被加载时,就做

        # __path__ must be set on the module, otherwise submodules
        # will not be loaded using the _ExtensionFinder.
        if not hasattr(mod, '__path__'):
            mod.__path__ = []

这也适用于根模块mypackage。这是完整的mypackage.py文件。

__all__ = []

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Extension import hook
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

import sys
import imp
import importlib

# The __path__ variable must be set in the module, otherwise the
# _ExtensionFinder will not be invoked for sub-modules.
__path__ = []

class _ExtensionFinder(object):
    """
    This class implements finding extension modules being imported by
    a prefix. The dots in the prefix are converted to underscores and
    will then be imported.

    .. seealso:: PEP 302 -- New Import Hooks
    """

    def __init__(self, prefix):
        super(_ExtensionFinder, self).__init__()
        self.prefix = prefix

    def _check_name(self, fullname):
        if self.prefix == fullname:
            return 1
        elif fullname.startswith(self.prefix + '.'):
            return 2
        return 0

    def _transform_name(self, fullname):
        value = self._check_name(fullname)
        if value == 1:
            return fullname
        elif value == 2:
            newpart = fullname[len(self.prefix) + 1:]
            newname = self.prefix.replace('.', '_') + '_' + newpart
            return newname
        return None

    def find_module(self, fullname, path=None):
        if self._check_name(fullname):
            return self

    def load_module(self, fullname):
        # If there is an existing module object named 'fullname'
        # in sys.modules, the loader must use that existing module.
        if fullname in sys.modules:
            return sys.modules[fullname]

        # Get the name of the module that actually needs to be
        # imported for this extension.
        newname = self._transform_name(fullname)

        # If it already exists, just fill up the missing entry for
        # the extension name.
        if newname in sys.modules:
            sys.modules[fullname] = sys.modules[newname]
            return sys.modules[newname]

        # Load the module and add additional attributes.
        mod = importlib.import_module(newname)
        mod.__loader__ = self

        # __path__ must be set on the module, otherwise submodules
        # will not be loaded using the _ExtensionFinder.
        if not hasattr(mod, '__path__'):
            mod.__path__ = []

        # The returned module must have a __name__, __file__
        # and __package__ attribute.
        assert all(hasattr(mod, n) for n in ['__name__', '__file__', '__package__'])

        return mod

_ext_finder = _ExtensionFinder('mypackage')
sys.meta_path.append(_ext_finder)
于 2014-10-02T19:43:22.167 回答