'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
    parent_fqname = globals['__name__']
    if globals['__ispkg__']:
        parent = sys.modules[parent_fqname]
        assert (globals is parent.__dict__)
        return parent
    i = parent_fqname.rfind('.')
    if (i == (-1)):
        return None
    parent_fqname = parent_fqname[:i]
    parent = sys.modules[parent_fqname]
    assert (parent.__name__ == parent_fqname)
    return parent
