##// END OF EJS Templates
embed_kernel: only import zmq when needed
Scott Tsai -
Show More
@@ -44,11 +44,6 b' from .config.loader import Config'
44 from .core import release
44 from .core import release
45 from .core.application import Application
45 from .core.application import Application
46 from .frontend.terminal.embed import embed
46 from .frontend.terminal.embed import embed
47 try:
48 from .zmq.ipkernel import embed_kernel
49 except ImportError:
50 def embed_kernel(*args, **kwargs):
51 raise ImportError("IPython.embed_kernel requires pyzmq >= 2.1.4")
52
47
53 from .core.error import TryNext
48 from .core.error import TryNext
54 from .core.interactiveshell import InteractiveShell
49 from .core.interactiveshell import InteractiveShell
@@ -61,3 +56,21 b' for author, email in release.authors.itervalues():'
61 __author__ += author + ' <' + email + '>\n'
56 __author__ += author + ' <' + email + '>\n'
62 __license__ = release.license
57 __license__ = release.license
63 __version__ = release.version
58 __version__ = release.version
59
60 def caller_module_and_locals():
61 """Returns (module, locals) of the caller"""
62 caller = sys._getframe(2)
63 global_ns = caller.f_globals
64 module = sys.modules[global_ns['__name__']]
65 return (module, caller.f_locals)
66
67 def embed_kernel(module=None, local_ns=None):
68 """Call this to embed an IPython kernel at the current point in your program. """
69 (caller_module, caller_locals) = caller_module_and_locals()
70 if module is None:
71 module = caller_module
72 if local_ns is None:
73 local_ns = caller_locals
74 # Only import .zmq when we really need it
75 from .zmq.ipkernel import embed_kernel as real_embed_kernel
76 real_embed_kernel(module, local_ns)
@@ -645,20 +645,7 b' def launch_kernel(*args, **kwargs):'
645 return base_launch_kernel('from IPython.zmq.ipkernel import main; main()',
645 return base_launch_kernel('from IPython.zmq.ipkernel import main; main()',
646 *args, **kwargs)
646 *args, **kwargs)
647
647
648 def caller_module_and_locals():
648 def embed_kernel(module, local_ns):
649 """Returns (module, locals) of the caller"""
650 caller = sys._getframe(1).f_back
651 global_ns = caller.f_globals
652 module = sys.modules[global_ns['__name__']]
653 return (module, caller.f_locals)
654
655 def embed_kernel(module=None, local_ns=None):
656 """Call this to embed an IPython kernel at the current point in your program. """
657 (caller_module, caller_locals) = caller_module_and_locals()
658 if module is None:
659 module = caller_module
660 if local_ns is None:
661 local_ns = caller_locals
662 app = IPKernelApp.instance(user_module=module, user_ns=local_ns)
649 app = IPKernelApp.instance(user_module=module, user_ns=local_ns)
663 app.initialize([])
650 app.initialize([])
664 app.start()
651 app.start()
General Comments 0
You need to be logged in to leave comments. Login now