5

I'm using IPython in zsh shell and I'm having a trouble defining a function with multiple lines.

For example, in the original Python shell, I can define a function f like this:

>>> def f(x):
...     a = x + 1
...     return a
... 
>>>

When I did it in IPython, the defination ends immediately when The line 'a = x + 1' ends. It looks like this:

In [4]: def f(x): 
   ...:     a = x + 1                                                                                                                              

In [5]:   

How can I fix it by changing some configuration or installing another version of IPython?

My version of Python and IPython:

Python 3.5.6 |Anaconda, Inc.|  
Type 'copyright', 'credits' or 'license' for more information
IPython 7.0.1 -- An enhanced Interactive Python. Type '?' for help.
gboffi
  • 22,939
  • 8
  • 54
  • 85
shb
  • 51
  • 3
  • Sorry if my question is dumb but... Why don't you put your script into a file and then execute it? – Pitto Oct 12 '18 at 08:47
  • Possible duplicate of [Adding line breaks in ipython](https://stackoverflow.com/questions/13536370/adding-line-breaks-in-ipython) – Simon Fromme Oct 12 '18 at 10:04
  • Are you using IPython 7.0.1 from the Anaconda distribution? It's really new and I'm seeing the same behavior — I expect that this will be fixed asap, so `conda update --all` could be everything that is needed to fix the problem. – gboffi Oct 12 '18 at 10:08
  • @SimonFromme The Q&A you referenced is from 2012 and very much water has passed under the bridge of multiline editing in IPython... so no, I don't think it's a duplicate of the present question – gboffi Oct 12 '18 at 10:12
  • 2
    @Pitto Because there is this fresh new idea of interactive programming: you modify, in the enhanced interpreter, your function definition then re-execute it using the command history, things like that... if you have not been exposed to [IPython](https://ipython.readthedocs.io/en/stable/overview.html) then you should _absolutely_ try it — also the [notebook](http://jupyter.org/) is a great feature – gboffi Oct 12 '18 at 10:16
  • I know notbooks well and I see your point, @gboffi but using the interpreter to write code, imho, is just a bad idea. I'd use it only to quickly jot a tiny snippet, maybe. – Pitto Oct 12 '18 at 11:13

1 Answers1

7

It's a known bug (see also this).

As it's clear from the issues I've referenced, it's not a bug with the Anaconda distribution but something that slipped into the 7.0 release of IPython.

Matthias Bussonier suggested to use C-o (that is Control o) as a stop gap measure. C-o opens new blank lines below the current line that you can reach and edit using the arrow keys — it's not a very satisfactory solution but for now there is no other solution (except, of course, downgrading IPython to 6.x).

Concluding, use C-o to open new lines and wait for a bug fix or downgrade to IPython 6.x.


Additional Info

Carlos Cordoba, one of the Spyder maintainers, in a comment to an answer of mine says that also the qtconsole is affected by this bug...

Moreover the bug, fixed in the IPython 7.1 shell, is still present in the same version of qtconsole and the stop-gap measure I've mentioned (Ctrl-o to open a line below the current one) does not work in qtconsole.

gboffi
  • 22,939
  • 8
  • 54
  • 85