'Returns the context in which a module should be imported.
The context could be a loaded (package) module and the imported module
will be looked for within that package. The context could also be None,
meaning there is no context -- the module should be looked for as a
"top-level" module.'

def _determine_import_context(self, globals):

    if ((not globals) or (not globals.get('__importer__'))):
        return None
    else:
        parent_fqname = globals['__name__']
        if globals['__ispkg__']:
            parent = sys.modules[parent_fqname]
            return parent
        i = parent_fqname.rfind('.')
        if (i == (-1)):
            return None
        parent_fqname = parent_fqname[:i]
        parent = sys.modules[parent_fqname]
        return parent
