__init__.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. # encoding: utf-8
  2. """
  3. IPython: tools for interactive and parallel computing in Python.
  4. http://ipython.org
  5. """
  6. #-----------------------------------------------------------------------------
  7. # Copyright (c) 2008-2011, IPython Development Team.
  8. # Copyright (c) 2001-2007, Fernando Perez <fernando.perez@colorado.edu>
  9. # Copyright (c) 2001, Janko Hauser <jhauser@zscout.de>
  10. # Copyright (c) 2001, Nathaniel Gray <n8gray@caltech.edu>
  11. #
  12. # Distributed under the terms of the Modified BSD License.
  13. #
  14. # The full license is in the file COPYING.txt, distributed with this software.
  15. #-----------------------------------------------------------------------------
  16. #-----------------------------------------------------------------------------
  17. # Imports
  18. #-----------------------------------------------------------------------------
  19. from __future__ import absolute_import
  20. import os
  21. import sys
  22. import warnings
  23. #-----------------------------------------------------------------------------
  24. # Setup everything
  25. #-----------------------------------------------------------------------------
  26. # Don't forget to also update setup.py when this changes!
  27. v = sys.version_info
  28. if v[:2] < (2,7) or (v[0] >= 3 and v[:2] < (3,3)):
  29. raise ImportError('IPython requires Python version 2.7 or 3.3 or above.')
  30. del v
  31. # Make it easy to import extensions - they are always directly on pythonpath.
  32. # Therefore, non-IPython modules can be added to extensions directory.
  33. # This should probably be in ipapp.py.
  34. sys.path.append(os.path.join(os.path.dirname(__file__), "extensions"))
  35. #-----------------------------------------------------------------------------
  36. # Setup the top level names
  37. #-----------------------------------------------------------------------------
  38. from .core.getipython import get_ipython
  39. from .core import release
  40. from .core.application import Application
  41. from .terminal.embed import embed
  42. from .core.interactiveshell import InteractiveShell
  43. from .testing import test
  44. from .utils.sysinfo import sys_info
  45. from .utils.frame import extract_module_locals
  46. # Release data
  47. __author__ = '%s <%s>' % (release.author, release.author_email)
  48. __license__ = release.license
  49. __version__ = release.version
  50. version_info = release.version_info
  51. def embed_kernel(module=None, local_ns=None, **kwargs):
  52. """Embed and start an IPython kernel in a given scope.
  53. If you don't want the kernel to initialize the namespace
  54. from the scope of the surrounding function,
  55. and/or you want to load full IPython configuration,
  56. you probably want `IPython.start_kernel()` instead.
  57. Parameters
  58. ----------
  59. module : ModuleType, optional
  60. The module to load into IPython globals (default: caller)
  61. local_ns : dict, optional
  62. The namespace to load into IPython user namespace (default: caller)
  63. kwargs : various, optional
  64. Further keyword args are relayed to the IPKernelApp constructor,
  65. allowing configuration of the Kernel. Will only have an effect
  66. on the first embed_kernel call for a given process.
  67. """
  68. (caller_module, caller_locals) = extract_module_locals(1)
  69. if module is None:
  70. module = caller_module
  71. if local_ns is None:
  72. local_ns = caller_locals
  73. # Only import .zmq when we really need it
  74. from ipykernel.embed import embed_kernel as real_embed_kernel
  75. real_embed_kernel(module=module, local_ns=local_ns, **kwargs)
  76. def start_ipython(argv=None, **kwargs):
  77. """Launch a normal IPython instance (as opposed to embedded)
  78. `IPython.embed()` puts a shell in a particular calling scope,
  79. such as a function or method for debugging purposes,
  80. which is often not desirable.
  81. `start_ipython()` does full, regular IPython initialization,
  82. including loading startup files, configuration, etc.
  83. much of which is skipped by `embed()`.
  84. This is a public API method, and will survive implementation changes.
  85. Parameters
  86. ----------
  87. argv : list or None, optional
  88. If unspecified or None, IPython will parse command-line options from sys.argv.
  89. To prevent any command-line parsing, pass an empty list: `argv=[]`.
  90. user_ns : dict, optional
  91. specify this dictionary to initialize the IPython user namespace with particular values.
  92. kwargs : various, optional
  93. Any other kwargs will be passed to the Application constructor,
  94. such as `config`.
  95. """
  96. from IPython.terminal.ipapp import launch_new_instance
  97. return launch_new_instance(argv=argv, **kwargs)
  98. def start_kernel(argv=None, **kwargs):
  99. """Launch a normal IPython kernel instance (as opposed to embedded)
  100. `IPython.embed_kernel()` puts a shell in a particular calling scope,
  101. such as a function or method for debugging purposes,
  102. which is often not desirable.
  103. `start_kernel()` does full, regular IPython initialization,
  104. including loading startup files, configuration, etc.
  105. much of which is skipped by `embed()`.
  106. Parameters
  107. ----------
  108. argv : list or None, optional
  109. If unspecified or None, IPython will parse command-line options from sys.argv.
  110. To prevent any command-line parsing, pass an empty list: `argv=[]`.
  111. user_ns : dict, optional
  112. specify this dictionary to initialize the IPython user namespace with particular values.
  113. kwargs : various, optional
  114. Any other kwargs will be passed to the Application constructor,
  115. such as `config`.
  116. """
  117. from IPython.kernel.zmq.kernelapp import launch_new_instance
  118. return launch_new_instance(argv=argv, **kwargs)