How can I run my currently edited file in a PyCharm console in a way that I can type into the command line afterwards?
Asked Answered
A

3

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

Assignation answered 16/12, 2013 at 11:16 Comment(3)
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.Charlacharlady
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.Assignation
possible duplicate of Running a module from the pycharm consoleWilliamwilliams
P
16

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

Prospector answered 16/12, 2013 at 11:28 Comment(5)
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.Assignation
@Assignation it's a known bug, should be fixed in the next update: youtrack.jetbrains.com/issue/PY-10769.Prospector
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.Assignation
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?Studding
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.Sternpost
M
17

Shift+Alt+Ewould execute the selected code.

Masseur answered 1/12, 2014 at 21:44 Comment(0)
P
16

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

Prospector answered 16/12, 2013 at 11:28 Comment(5)
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.Assignation
@Assignation it's a known bug, should be fixed in the next update: youtrack.jetbrains.com/issue/PY-10769.Prospector
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.Assignation
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?Studding
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.Sternpost
M
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!)

Meadows answered 3/4, 2015 at 17:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.