Repository: ipython/ipython

Issue:
Cannot load IPython extension with --simple-prompt
Mostly mean you cannot test IPython extensions like @asmeurer iterm2-tools, 
as as soon as you're not in a tty this fails. 

```
$ ipython --ext=iterm2_tools.ipython --simple-prompt
Python 3.5.1 |Anaconda custom (x86_64)| (default, Dec  7 2015, 11:24:55)
Type "copyright", "credits" or "license" for more information.

IPython 5.0.0.dev -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.
[TerminalIPythonApp] WARNING | Error in loading extension: iterm2_tools.ipython
Check your config files in /Users/bussonniermatthias/.ipython/profile_default
Traceback (most recent call last):
  File "/Users/bussonniermatthias/dev/ipython/IPython/core/shellapp.py", line 246, in init_extensions
    self.shell.extension_manager.load_extension(ext)
  File "/Users/bussonniermatthias/dev/ipython/IPython/core/extensions.py", line 85, in load_extension
    if self._call_load_ipython_extension(mod):
  File "/Users/bussonniermatthias/dev/ipython/IPython/core/extensions.py", line 132, in _call_load_ipython_extension
    mod.load_ipython_extension(self.shell)
  File "/Users/bussonniermatthias/dev/iterm2-tools/iterm2_tools/ipython.py", line 108, in load_ipython_extension
    load_ipython_extension_prompt_toolkit(ipython)
  File "/Users/bussonniermatthias/dev/iterm2-tools/iterm2_tools/ipython.py", line 140, in load_ipython_extension_prompt_toolkit
    ipython.prompts = wrap_prompts_class(Prompts)(ipython)
  File "/Users/bussonniermatthias/dev/traitlets/traitlets/traitlets.py", line 567, in __set__
    self.set(obj, value)
  File "/Users/bussonniermatthias/dev/traitlets/traitlets/traitlets.py", line 556, in set
    obj._notify_trait(self.name, old_value, new_value)
  File "/Users/bussonniermatthias/dev/traitlets/traitlets/traitlets.py", line 1102, in _notify_trait
    'type': 'change',
  File "/Users/bussonniermatthias/dev/traitlets/traitlets/traitlets.py", line 1136, in notify_change
    c(change)
  File "/Users/bussonniermatthias/dev/ipython/IPython/terminal/ptshell.py", line 110, in _
    self._update_layout()
  File "/Users/bussonniermatthias/dev/ipython/IPython/terminal/ptshell.py", line 285, in _update_layout
    self._app.layout = create_prompt_layout(**self._layout_options())
AttributeError: 'TerminalInteractiveShell' object has no attribute '_app'
```



Discussion:
It's not that loading extensions fails, it's that trying to manipulate the prompt toolkit stuff fails. Which is not really a surprise, given that simple-prompt disables that machinery.


Resolving patch:
diff --git a/IPython/terminal/ptshell.py b/IPython/terminal/ptshell.py
--- a/IPython/terminal/ptshell.py
+++ b/IPython/terminal/ptshell.py
@@ -134,6 +134,7 @@ def init_term_title(self, change=None):
             toggle_set_term_title(False)
 
     def init_prompt_toolkit_cli(self):
+        self._app = None
         if self.simple_prompt:
             # Fall back to plain non-interactive output for tests.
             # This is very limited, and only accepts a single line.
@@ -282,7 +283,8 @@ def _update_layout(self):
         Ask for a re computation of the application layout, if for example ,
         some configuration options have changed.
         """
-        self._app.layout = create_prompt_layout(**self._layout_options())
+        if self._app:
+            self._app.layout = create_prompt_layout(**self._layout_options())
 
     def prompt_for_code(self):
         document = self.pt_cli.run(


Test patch: