How can I run selected lines in Spyder 4?

Question:

In previous versions of Spyder, you can select lines in the editor, and only run those selected lines.

In Spyder 4, when you select lines and press Ctrl+Enter it executes runcell(0, '/your/dir/file.py') which runs the whole code.

How can I run just the lines which I have selected?

Asked By: Guinther Kovalski

||

Answers:

The hotkey for running a the line at which the cursor is, or the currently selected lines in Spyder 4 is F9. Ctrl+Enter will run the current cell. You can read more about cells in Spyder here. Basically, these are blocks of code that can be defined in the editor using a specific syntax within the code, and can be run independently by Spyder and other IDEs.

Incidentally, Ctrl+Enter is the keyboard shortcut for running the current line in R Studio, not Spyder, which may be where you got it confused.

Answered By: CDJB

In Spyder 4.0.1, the keyboard shortcut for ‘run cell’ is set to Ctrl+Return but for ‘run selection’ it is set to F9. You can use F9 to run a selection or if you prefer to use Ctrl+Return, you can go to Tools -> Preferences -> Keyboard shortcuts. Search for ‘run selection’, double click and set Ctrl+Return as the ‘New shortcut’

Answered By: Ranjitha

In Spyder 4 the keyboard shortcut to run the highlighted lines of code is F9. and if you want to use any other shortcut then you can change it from TOOLS>>PREFERENCES>>KEYBOARD SHORTCUTS then double click on any shortcut you want to change and change with your shortcut. but be very careful if you used the same shortcut that is used for any other work then for that it will remove.

Answered By: Deepesh ranjan

In Spyder, you can split your code into runnable cells

Use the the pattern # In[] or #%% to define the start of a cell. You can also have child cells by adding additional percent signs %

Spyder iPython Cells

Answered By: Abdulrahman Bres
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.