'Python calls this hook to locate and import a module.'

def _import_hook(self, fqname, globals=None, locals=None, fromlist=None):

    parts = fqname.split('.')
    parent = self._determine_import_context(globals)
    if parent:
        module = parent.__importer__._do_import(parent, parts, fromlist)
        if module:
            return module
    try:
        top_module = sys.modules[parts[0]]
    except KeyError:
        top_module = self._import_top_module(parts[0])
        if (not top_module):
            raise ImportError, ('No    module    named    ' + fqname)
    if (len(parts) == 1):
        if (not fromlist):
            return top_module
        if (not top_module.__dict__.get('__ispkg__')):
            return top_module
    importer = top_module.__dict__.get('__importer__')
    if importer:
        return importer._finish_import(top_module, parts[1:], fromlist)
    if ((len(parts) == 2) and hasattr(top_module, parts[1])):
        if fromlist:
            return getattr(top_module, parts[1])
        else:
            return top_module
    raise ImportError, ('No    module    named    ' + fqname)
