pythonipythoncellexecution

Forcing IPython to execute the current multiline code block


In IPython (7.3.0), I can edit a multiline code block:

In [1]: def funtcion(a, b):
   ...:     return a + b
   ...:

I then realize that I've made a typo, which I want to correct (| marks the location of the cursor):

<press up arrow, then edit>
In [2]: def funct|ion(a, b):
   ...:     return a + b
   ...:

I want to execute that:

<press enter>
In [2]: def funct
   ...: |ion(a, b):
   ...:     return a + b
   ...:

...which doesn't really do what I want.

Is there a way to force IPython to execute the current cell?


Solution

  • As @AntiMatterDynamite has mentioned in the comments, you could navigate to the end of the block and then hit Enter. But (for me, at least) that seems to miss the point.

    Instead, try hitting Shift+Enter mid line, which should execute the current cell regardless of cursor position. This has always worked for me in Spyder's IPython console.

    Interestingly, when I tested hitting Shift+Enter in a regular IPython console (ie. IPython initialised from the terminal, not from Spyder) it didn't work. However, hitting Esc followed by Enter did work.