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.]

3
  • 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, 2013 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. Dec 16, 2013 at 13:32
  • possible duplicate of Running a module from the pycharm console Jul 12, 2014 at 11:23

3 Answers 3

17

Shift+Alt+Ewould execute the selected code.

16

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

5
  • 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. Dec 16, 2013 at 12:40
  • 1
    @user3107036 it's a known bug, should be fixed in the next update: youtrack.jetbrains.com/issue/PY-10769.
    – CrazyCoder
    Dec 16, 2013 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. Dec 16, 2013 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? May 8, 2014 at 7: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. Jun 16, 2016 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!)

0

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.