28

I want this so I can retain the command line history after repeated runs, and to paste lines from the console into tests etc. Exactly like in IDLE.

[I realize this question is basically a duplicate of Running a module from the pycharm console. But the question there is not answered satisfyingly (for me), and my lack of reputation does not let me comment there, since I just signed up.]

Community
  • 1
  • 1
user3107036
  • 426
  • 1
  • 5
  • 10
  • 1
    Open a console from `tools->Run python console` import your module using `from _ import _` and call your class or methods, If you have a `main()` then just call `main()`, if you are getting a specific error just paste it and we'll try to help. – Kobi K Dec 16 '13 at 11:22
  • Thanks, but what I'd like to do is to //execute// the module repeatedly while editing. Importing makes me fiddle with reload etc. Furthermore, the module is in a sub-folder of the project, so I have to type in its full path. I wish this simple function would be built into PyCharm. I still don't know whether it is or not. Perhaps I should post a feature request. – user3107036 Dec 16 '13 at 13:32
  • possible duplicate of [Running a module from the pycharm console](http://stackoverflow.com/questions/16874046/running-a-module-from-the-pycharm-console) – Piotr Dobrogost Jul 12 '14 at 11:23

3 Answers3

17

Shift+Alt+Ewould execute the selected code.

user3507584
  • 3,246
  • 5
  • 42
  • 66
16

Select the code fragment or the entire file, then use Execute Selection in Console from the context menu.

CrazyCoder
  • 389,263
  • 172
  • 990
  • 904
  • 1
    This is a great feature, but unfortunately it seems to work only with single statements (PyCharm CE 3.0.2, Mac OS). Selecting and running a single function is fine, but selecting the entire file (which otherwise executes without problems) results in lots of errors. – user3107036 Dec 16 '13 at 12:40
  • 1
    @user3107036 it's a known bug, should be fixed in the next update: http://youtrack.jetbrains.com/issue/PY-10769. – CrazyCoder Dec 16 '13 at 12:41
  • 4
    Ah, thanks. There's light at the end of the tunnel.. ;) Since you seem to work for JetBrains, may I suggest to put this feature into the context menu of the respective editor tab, too? This way, you wouldn't have to Select All all the time. – user3107036 Dec 16 '13 at 13:35
  • I don't see "Execute Selection in Console" in any context menu. I'm using PyCharm 3.1.2 CE. Has the feature been removed? – fantabolous May 08 '14 at 07:50
  • I have a code that uses the __file__ attribute of the script, so this option doesn't work. I guess using debug and breakpoints is the only option in my case. – derchambers Jun 16 '16 at 22:37
8

For anyone still having this problem: Go to the Run/Debug menu, choose Edit Configuration, check the box 'Show command line' this will enable you to enter parameters in the console at the >>> prompt and test your function.

Edit: To make this change apply to all your .py files (as this check box only applies to the current file you're working on) go to: Edit configuration, in the pop up you will see a menu tree on the left, select Defaults, then Python, then check the 'Show command line' box, this will make it the default setting whenever you open a .py file, (this feature should really be on by default!)

Rampkins
  • 371
  • 4
  • 5