Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I run selected lines in Spyder 4?

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?

like image 493
Guinther Kovalski Avatar asked Dec 10 '19 12:12

Guinther Kovalski


2 Answers

In Spyder, you can organize your code as runnable cells.

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

Spyder iPython Cells

like image 66
Abdulrahman Bres Avatar answered Oct 24 '22 12:10

Abdulrahman Bres


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.

like image 25
CDJB Avatar answered Oct 24 '22 11:10

CDJB