__init__.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. # PYTHON_ARGCOMPLETE_OK
  2. """
  3. IPython: tools for interactive and parallel computing in Python.
  4. https://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. import sys
  20. #-----------------------------------------------------------------------------
  21. # Setup everything
  22. #-----------------------------------------------------------------------------
  23. # Don't forget to also update setup.py when this changes!
  24. if sys.version_info < (3, 10):
  25. raise ImportError(
  26. """
  27. IPython 8.19+ supports Python 3.10 and above, following SPEC0.
  28. IPython 8.13+ supports Python 3.9 and above, following NEP 29.
  29. IPython 8.0-8.12 supports Python 3.8 and above, following NEP 29.
  30. When using Python 2.7, please install IPython 5.x LTS Long Term Support version.
  31. Python 3.3 and 3.4 were supported up to IPython 6.x.
  32. Python 3.5 was supported with IPython 7.0 to 7.9.
  33. Python 3.6 was supported with IPython up to 7.16.
  34. Python 3.7 was still supported with the 7.x branch.
  35. See IPython `README.rst` file for more information:
  36. https://github.com/ipython/ipython/blob/main/README.rst
  37. """
  38. )
  39. #-----------------------------------------------------------------------------
  40. # Setup the top level names
  41. #-----------------------------------------------------------------------------
  42. from .core.getipython import get_ipython
  43. from .core import release
  44. from .core.application import Application
  45. from .terminal.embed import embed
  46. from .core.interactiveshell import InteractiveShell
  47. from .utils.sysinfo import sys_info
  48. from .utils.frame import extract_module_locals
  49. __all__ = ["start_ipython", "embed", "start_kernel", "embed_kernel"]
  50. # Release data
  51. __author__ = '%s <%s>' % (release.author, release.author_email)
  52. __license__ = release.license
  53. __version__ = release.version
  54. version_info = release.version_info
  55. # list of CVEs that should have been patched in this release.
  56. # this is informational and should not be relied upon.
  57. __patched_cves__ = {"CVE-2022-21699", "CVE-2023-24816"}
  58. def embed_kernel(module=None, local_ns=None, **kwargs):
  59. """Embed and start an IPython kernel in a given scope.
  60. If you don't want the kernel to initialize the namespace
  61. from the scope of the surrounding function,
  62. and/or you want to load full IPython configuration,
  63. you probably want `IPython.start_kernel()` instead.
  64. Parameters
  65. ----------
  66. module : types.ModuleType, optional
  67. The module to load into IPython globals (default: caller)
  68. local_ns : dict, optional
  69. The namespace to load into IPython user namespace (default: caller)
  70. **kwargs : various, optional
  71. Further keyword args are relayed to the IPKernelApp constructor,
  72. such as `config`, a traitlets :class:`Config` object (see :ref:`configure_start_ipython`),
  73. allowing configuration of the kernel (see :ref:`kernel_options`). Will only have an effect
  74. on the first embed_kernel call for a given process.
  75. """
  76. (caller_module, caller_locals) = extract_module_locals(1)
  77. if module is None:
  78. module = caller_module
  79. if local_ns is None:
  80. local_ns = caller_locals
  81. # Only import .zmq when we really need it
  82. from ipykernel.embed import embed_kernel as real_embed_kernel
  83. real_embed_kernel(module=module, local_ns=local_ns, **kwargs)
  84. def start_ipython(argv=None, **kwargs):
  85. """Launch a normal IPython instance (as opposed to embedded)
  86. `IPython.embed()` puts a shell in a particular calling scope,
  87. such as a function or method for debugging purposes,
  88. which is often not desirable.
  89. `start_ipython()` does full, regular IPython initialization,
  90. including loading startup files, configuration, etc.
  91. much of which is skipped by `embed()`.
  92. This is a public API method, and will survive implementation changes.
  93. Parameters
  94. ----------
  95. argv : list or None, optional
  96. If unspecified or None, IPython will parse command-line options from sys.argv.
  97. To prevent any command-line parsing, pass an empty list: `argv=[]`.
  98. user_ns : dict, optional
  99. specify this dictionary to initialize the IPython user namespace with particular values.
  100. **kwargs : various, optional
  101. Any other kwargs will be passed to the Application constructor,
  102. such as `config`, a traitlets :class:`Config` object (see :ref:`configure_start_ipython`),
  103. allowing configuration of the instance (see :ref:`terminal_options`).
  104. """
  105. from IPython.terminal.ipapp import launch_new_instance
  106. return launch_new_instance(argv=argv, **kwargs)
  107. def start_kernel(argv=None, **kwargs):
  108. """Launch a normal IPython kernel instance (as opposed to embedded)
  109. `IPython.embed_kernel()` puts a shell in a particular calling scope,
  110. such as a function or method for debugging purposes,
  111. which is often not desirable.
  112. `start_kernel()` does full, regular IPython initialization,
  113. including loading startup files, configuration, etc.
  114. much of which is skipped by `embed_kernel()`.
  115. Parameters
  116. ----------
  117. argv : list or None, optional
  118. If unspecified or None, IPython will parse command-line options from sys.argv.
  119. To prevent any command-line parsing, pass an empty list: `argv=[]`.
  120. user_ns : dict, optional
  121. specify this dictionary to initialize the IPython user namespace with particular values.
  122. **kwargs : various, optional
  123. Any other kwargs will be passed to the Application constructor,
  124. such as `config`, a traitlets :class:`Config` object (see :ref:`configure_start_ipython`),
  125. allowing configuration of the kernel (see :ref:`kernel_options`).
  126. """
  127. import warnings
  128. warnings.warn(
  129. "start_kernel is deprecated since IPython 8.0, use from `ipykernel.kernelapp.launch_new_instance`",
  130. DeprecationWarning,
  131. stacklevel=2,
  132. )
  133. from ipykernel.kernelapp import launch_new_instance
  134. return launch_new_instance(argv=argv, **kwargs)