PEP 302 -- New Import Hooks指定挂钩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文件中安装finder类,但我的目标是将其安装在根包mypackage
中。
查找程序将通过.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
编辑:我发现描述我想要实现的目标的正确词是&#34;命名空间包&#34;
答案 0 :(得分: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)