From ec08632ffc023118751951cb00429b9dff6b8cb5 2016-04-13 13:44:51 From: Matthias Bussonnier Date: 2016-04-13 13:44:51 Subject: [PATCH] Merge pull request #9392 from minrk/embed-module-may-not-exist handle failure to get module for globals --- diff --git a/IPython/terminal/embed.py b/IPython/terminal/embed.py index f78aa2a..aa43ca4 100644 --- a/IPython/terminal/embed.py +++ b/IPython/terminal/embed.py @@ -197,7 +197,14 @@ class InteractiveShellEmbed(TerminalInteractiveShell): local_ns = call_frame.f_locals if module is None: global_ns = call_frame.f_globals - module = sys.modules[global_ns['__name__']] + try: + module = sys.modules[global_ns['__name__']] + except KeyError: + warnings.warn("Failed to get module %s" % \ + global_ns.get('__name__', 'unknown module') + ) + module = DummyMod() + module.__dict__ = global_ns if compile_flags is None: compile_flags = (call_frame.f_code.co_flags & compilerop.PyCF_MASK)