__init__.py 5.6 KB

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