__init__.py 6.4 KB

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