1

I have just started learning Python. I've been using R to do data analysis. One of the good things about RStudio is that we can select a few statements and press Ctrl+Enter to execute those lines. RStudio would show the output for each line.

For instance, the output of these lines:

a<-2
b<-3
a+1
b+2

would be

> a<-2
> b<-3
> a+1
[1] 3
> b+2
[1] 5

Please note that R, by default, would print all lines--not just the last line. This is immensely helpful when executing long scripts.

I learned from another thread and this thread that we can simulate above in Python by adding the following statements:

from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"

This works well. For instance, here's input and output:

from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"

from fractions import Fraction
import fractions
z = fractions.Fraction(1,3)
x = Fraction(1,3)
y = Fraction(2,3)
Fraction('0.25')
x+y
Out[50]: Fraction(1, 4)
Out[50]: Fraction(1, 1)

I have one question:

a) Adding InteractiveShell.ast_node_interactivity every time I execute my code is really annoying. Is there any way I can make this auto-executable (like .RProfile file in RStudio) when I start my PyCharm? (In RStudio, we have .RProfile file in which we can add all customizations. I prefer using PyCharm because of a lot of inbuilt features.)

One of the recommendations from SO threads was to modify ..\.ipython\profile_default and add above lines (i.e. ...ast_node_interactivity...) in .py file in Windows. Unfortunately, I have added above statements, and it wouldn't print the output of all statements in PyCharm. I still have to add those two lines to every .py file in PyCharm to see what's happening in my script. I am extremely frustrated with this.

Another recommendation was to add ;. This is annoying to me as well because then I have separate, say, 50 lines by ;, which would be difficult to read. Also, one has to remember to add ;, which is not good.

I am an absolute beginner. So, I'd appreciate any thoughts. I have googled this topic as well, and it seems there isn't much discussion on this topic.


I am adding clarification: I want to use PyCharm's Execute Selection in Console which has keyboard shortcut Alt + Shift + E in Windows. One would have to select a bunch of line of code and then see the output for each line (just as RStudio does). This helps when debugging a long script. As it is, it only prints the output of the last line if I don't add above two lines.

watchtower
  • 4,140
  • 14
  • 50
  • 92
  • If I understand your question correctly, you can use the stepping tools provided by Pycharm found under the Run menu to step through your program to each execution point. – arkdevelopment Dec 26 '17 at 21:53
  • @arkdev. Thanks for your help. I want to use PyCharm's `Execute Selection in Console` which has keyboard shortcut `Alt + Shift + E`. One would have to select a bunch of lines and then see the output. This helps when debugging a long script. Also, if I am not wrong, to use Run + Debug + Step, one would have to run the entire script and set a breakpoint. I want to run only 2-3 lines of code to see what happens, which is what `Execute Selection in Console` provides. – watchtower Dec 26 '17 at 21:57

0 Answers0