__import__ already has a 'level' argument.

doh! maybe "context" is better, then.
 
Not without frame inspection to know what import statements are in the context manager's block.


I don't doubt you know what you're talking about, so this is a learning question: why couldn't you do it by simply adding an "inside_context_manager" flag, and producing an appropriate globals and locals based on the flag value?

this is really lame, but maybe like this:

class __import__:

    def __enter__(self):
        self.inside_context_manager = True
        return None

    def __exit__(self, *args):
        self.inside_context_manager = False

    def __call__(self, name, globals=None, locals=None, fromlist=(), level=0, context=None):
        am_i_outside_context_manager =  not self.inside_context_manager
        if am_i_outside_context_manager:
            do_normal_import(name, globals, locals, fromlist, level)
        else:
            # inside context manager
            if context is None:
                raise ValueError("invalid import context")
            cglobals = contextualized_globals(globals, context)
            clocals = contextualized_locals(locals, context)
            do_special_import(name, cglobals, clocals, fromlist, level)