question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Execute multiple lines of code at once

See original GitHub issue

Hi, I am trying to execute multiple lines of code using ipython 6.0.1 (I also have tried 6.0.0) but cannot find any shortcut working (i.e. alt + enter or shift + enter) to get a new line. I am not sure if this is a bug or if I am missing something? For example if I want to write and execute these multiple lines of code at once:

In [5]: import numpy as np
   ...: a = np.array([1])
   ...: print(a)
   ...: 
[1]

At the moment I need to explicitly write the backslash operator to get a new line, i.e.

In [4]: import numpy as np \
   ...: a = np.array([1]) \
   ...: print(a)

and then remove manually the backslash operators to make it run as in the first example, otherwise ipython is complaining:

In [6]: import numpy as np \
   ...: a = np.array([1]) \
   ...: print(a)
  File "<ipython-input-6-b0ea1b71283b>", line 1
    import numpy as np a = np.array([1]) print(a)
                       ^
SyntaxError: invalid syntax

I have tried 2 different terminals but get the same problem. Any help would be appreciated!

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

7reactions
ghostcommented, Jun 15, 2018

There is a trick you can use. You can use parenthesis in the beginning of the code-block you want to write, when hit enter-key, it will automatically add lines to your command meanwhile the parenthesis in the beginning is not closed. Before executing you must remove parenthesis. Another common way it’s if you have a block defined by after ‘:’, everytime you hit enter-key it will add conforming the identetion of the block (which is nice if you need to write and execute a for loop). If you add a line with any of these manners and in the final line you hit enter (and this line is empty and all parenthesis previously closed if any) it will execute the whole block you wrote without problems. You can try and discover many other ways. But it’s possible. You don’t have to argue with one-line convention if needed a multiple-line execution or block one

7reactions
maria8899commented, Jun 1, 2017

Thanks, I see. I thought it was something I was missing as it is what is shown on the main documentation in the picture: https://ipython.readthedocs.io/en/stable/_images/ipython-6-screenshot.png

In the meantime I have found out a shortcut that is somewhat working (in my settings ?) which is ctrl+o. This actually starts a new line (with the 3 dots ‘…’), except the cursor is staying at the same place and I need to press the arrow key to put the cursor on the new line. This is not the best but still better than the backslash!

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Execute Multiple Lines in a Single Line Python From ...
Summary: To make a Python one-liner out of any multi-line Python script, replace the new lines with a new line character '\n' and...
Read more >
dHow to execute multiple lines of code, Python, Selenium
I have all the webscraping done, all the send_keys done. I just want to be able to execute multiple lines of code as...
Read more >
How do I run a multi-line Python script in IDLE? - Quora
Python, writing multi line code in IDLE, Try File => New Window in top menu. Then write your code in this windows and...
Read more >
Enter Multiple Lines Without Running Them - MatLab
To enter multiple lines before running any of them, use Shift+Enter or Shift+Return after typing a line. This is useful, for example, when...
Read more >
How to Comment Out Multiple Lines in Python - freeCodeCamp
Apart from making your code more readable, comments can also help ... two lines of code, you can comment out one to prevent...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found