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