Epimetheus
Epimetheus

Reputation: 1147

What is the difference between ipython TerminalInteractiveShell and InteractiveShell?

The ipython documentation has a long list of configurable options. Some are for the TerminalInteractiveShell, some are for the InteractiveShell. Some options appear in one in the documentation and in the other when I generate a ipython_config.py from the command line. Is there any rhyme or reason to this? What is the reason for having both?

Upvotes: 0

Views: 671

Answers (1)

anji
anji

Reputation: 46

Looking through the list of modules in the docs, we have

IPython.core.application: An application for IPython.

IPython.core.interactiveshell: Main IPython class.

IPython.terminal.ipapp: The Application object for the command line ipython program.

IPython.terminal.interactiveshell: IPython terminal interface using prompt_toolkit

InteractiveShell is the base iPython shell class, which can be subclassed for different purposes. TerminalInteractiveShell is one such specific subclass. It inherits from InteractiveShell, and is the specific shell that runs when we type "ipython" into the command line.

One example of a config option that's available for TerminalInteractiveShell but not the base InteractiveShell is confirm_exit. This seems reasonable, since the "Do you really want to exit" question is only really relevant when there's a human pressing Control-D inside the shell.

Practically, if you're just trying to configure your command line ipython shell, changing either the InteractiveShell or TerminalInteractiveShell version of an option would work because TerminalInteractiveShell inherits the base class's options. However, it's safer to only edit the more specific TerminalInteractiveShell ones, since the base class is still used in non-terminal situations, and changing its settings could lead to unexpected behavior there. For example:

  • This Jupyter Notebook tutorial uses InteractiveShell to transform a notebook code cell into executable Python

  • This test class in the ipython source code creates a shell (inheriting from plain InteractiveShell) for each test.

Hope this was helpful!

Upvotes: 3

Related Questions