I usually use RStudio to code in R, and when running code line by line, if running the first line of a loop, the content of the loop runs with it. It is very convenient for debugging code, to be able to run code line by line without manually selecting the whole loop for example. In Spyder, if I run the first line of a loop, it runs just that, as if I was trying to run an empty loop, and gives an error. How can a run a code line by line properly in Spyder? I have researched the question but did not find an answer. Thank you for your help!
Asked
Active
Viewed 7,570 times
6
-
I belive this is the answer: https://stackoverflow.com/a/39037466/8905101 In short,f9. – Ethan Henderson Jan 24 '18 at 19:44
-
No, this indeed runs the code line by line, but as I mentioned it only runs the line and if that line is for example the first line of a loop, it will not run the loop content, and will output an error – arielle Jan 24 '18 at 19:45
-
Are you saying you want to run the entire loop by only highlighting the "for x in iter:" Is the loop too long to highlight the whole thing? Or are you asking if highlighting code and pressing shft+enter will run your code? – lwileczek Jan 24 '18 at 19:46
-
Not by highlighting the "for x in iter", just having the cursor in that line. In RStudio, if the cursor is in that line and I run the line, it runs the loop content along with it. – arielle Jan 24 '18 at 19:50
-
And yes it is an issue for particularly long loops, or just when trying to run several lines of code quickly – arielle Jan 24 '18 at 19:51
-
1You should go in Debugging mode (Ctrl+F5). Then you can run your code line-by-line with "Run current line" button (or Ctrl+F10). Including loops. – Vadim Shkaberda Jan 24 '18 at 20:40
-
In debugging mode is the shortcut to run line by line supposed to be the same as the one you would use in normal mode? I have set my shortcut to be cmd+enter (I use a mac). When I try that in debug mode, the cursor moves to the next line, but there is no output in the console like there normally would be. – arielle Jan 24 '18 at 20:48
1 Answers
4
(Spyder developer here) This problem has been reported before but we haven't had time to solve it. Although it sounds simple, it's really hard to solve correctly in all situations because indentation spaces matters in Python (whereas in R they are irrelevant).

Carlos Cordoba
- 33,273
- 10
- 95
- 124
-
I see, thanks for letting me know! I hope this will be added sometime in the future. What about using debugging mode like another user mentioned above, as a comment on my question? – arielle Jan 24 '18 at 20:58
-
1You can do that right now. In other words, we don't need to add anything new to Spyder for it to work. – Carlos Cordoba Jan 24 '18 at 21:20
-
As I mentioned, I cannot get that to work: "In debugging mode is the shortcut to run line by line supposed to be the same as the one you would use in normal mode? I have set my shortcut to be cmd+enter (I use a mac). When I try that in debug mode, the cursor moves to the next line, but there is no output in the console like there normally would be". What I am doing wrong? Thank you – arielle Jan 24 '18 at 21:29
-
You're not doing anything wrong. Things work differently in debug mode, but you can move line by line with it. The difference is that debug mode is very limited because you can only evaluate single line expressions, you don't have syntax highlighting of your code, and you can't evaluate any Python expression (you have to precede any statement with `!`). At the end, things don't work as simple as in RStudio, sorry. – Carlos Cordoba Jan 24 '18 at 22:01