27

I am reading the book Python Data Science Handbook by Jake VanderPlas. In the first chapter a multi line input in IPython is illustrated:

multiLineInput

Can anybody please tell me how to do that? I know how to write a block before executing it in Jupyter Notebook, but in the IPython shell I don't know how to do it.

SuperStormer
  • 4,997
  • 5
  • 25
  • 35
aurumpurum
  • 932
  • 3
  • 11
  • 27

6 Answers6

56

Ctrl+o

If you type Ctrl+o, you should be able to add additional lines to the input to run them simultaneously.

If that doesn't work, as a workaround, you can paste multiple lines after copying them from elsewhere, or you can press enter on a line with a semicolon or unclosed parentheses. IPython will automatically give you another line to finish your statement rather than running and instantly hitting a syntax error.

Depending on the environment where you are running IPython, you might also want to try Ctrl+Enter or Shift+Enter.

SuperStormer
  • 4,997
  • 5
  • 25
  • 35
AreToo
  • 1,102
  • 11
  • 24
  • I am using in Pycharm and that does not work, however Ctrl + Enter does the job – Iqigai Jun 25 '22 at 11:19
  • (maOS Ventura, launching iPython 8.3 via Terminal from Bash prompt) Ctrl+O creates a new line just below the line where Ctrl+O is pressed and one needs to move the cursor using the down arrow key to the beginning of the newly created line. Wish there was a shortcut that does both. – Alper Apr 27 '23 at 07:36
3

Use 'shift' + 'Enter' at the end of line to avoid execution and keep typing your code. When you are done writing your block of code hit 'Enter' Have a look to the following picture If for some reason shift doesn't work, Ctrl + Enter instead. I hope this helps, good luck.

ZAnteneh
  • 39
  • 2
  • 2
    Thank you @ZAnteneh. The problem is that the author uses jupyter notebook, but that was not very clear for me...anyway, to go to a new line without executing, ctrl + o works fine for me (working on MacOS). – aurumpurum Nov 04 '19 at 15:14
  • 2
    Ctrl + O is the one which works in ubuntu - ipython v5.5.0 – Ajmal Moochingal May 10 '21 at 06:19
3

Add "<SPACE>\" at the end of the first line before hitting <ENTER>.

It allows you to enter more lines and you can remove the " \" while editing the multiline-input as well. Execute the input as described before by pressing <ENTER> in the last (empty) line, which may require pressing <ENTER> 2x at the end of the last non-empty line.

If the input already executes while pressing <ENTER> anywhere in the last non-empty line of the input you can still insert lines anywhere by pressing <ENTER> at the end of the line above and you can add a line at the end by adding "<SPACE>\" at the end of the last line as before.

For some reason none of the above methods worked for me in a cmd shell on Windows, but this did the trick.

e.d.n.a
  • 191
  • 1
  • 6
2

As an addition to the previous answers. you will need to press Enter twice to run the code. In Python 3.7.3 and maybe above, pressing enter just takes you to a new line of the multi-line ipython input. In order to run the code, press Enter again.

Tambe Tabitha
  • 119
  • 2
  • 4
1

Use Ctrl + "O" before hitting Enter

I had tried the same for above code.

Pushpendra
  • 97
  • 1
  • 3
-3

My suggestion is that rather using ipython shell, install jupyter notebook (pip install jupyter). then type jupyter notebook in the terminal rather than typing ipython. this command will open jupyter notebook in your default browser. here you can do whatever you want to do. you don't need have any internet connection. to stop the jupyter server, type ctrl+d in the terminal. you can watch the following tutorial on youtube: https://youtu.be/DKiI6NfSIe8

Enjoy.