'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?
Solution 1:[1]
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'
Solution 2:[2]
In Spyder, you can organize your code as 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 %.

Solution 3:[3]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Ranjitha |
| Solution 2 | |
| Solution 3 | Deepesh ranjan |
