Quick command to run file in console in Pycharm using the command runfile().
已回答
Hello!
Is there a quick way to use the runfile("file.py", wdir="path/to/working/dir") to launch an edited file or another file in the same console.
This would be nice to have coming from the IDE spyder.
Thanks in advance!
请先登录再写评论。
Hi! Since PyCharm 2017.3 there is an action "Execute File in Console" which does exactly that. You can find it with Find action (Ctrl + Shift + A) and execute currently selected file.
Hello!
Thanks for your answer!
Can't seem to find this command. Looking threw the key map and "Find action...", the "Execute File in Console" does not exists.
(I'm running the latest update on mac: PyCharm 2017.3.1 (Professional Edition) Build #PY-173.3942.36, built on December 13, 2017)
Thanks again!
Hi! I believe Elizabeth meant Run File in Console:
Hi Pavel
This action unfortunate does not execute the file with the "runfile" function in the console (which is the Spyder default).
It executes the file in the run window. The disadvantage of this is that you don't have an interactive console afterwards.
There is a work around by automatically starting a console after running the file (as can be set in the run configurations). However this is not working optimal since the console has to restart each time after the file is executed (cluttering the window with start-up text after output of the file itself) and also opening a new tab each time a file is executed.
In the picture below the desired behavior (typed manually). The "4: Run" in the bottom left corner is the default place to end up with by using a short cut to execute a file.
Would it be possible to bind a key combination to this behavior?
Note: Interestingly, when using the "alt + shift + e" short key (execute selection) the result does end up in the python console. So in combination with the "ctr + a" short key you are coming close to the desired behavior, although because the whole selection is copied to the console you still end up with cluttering the window (but at least the cluttering is before the actual print statements and not after).
Hi H Walinga! It looks like you're talking about usual "Run" action, which executes file in the run window. But if you execute action "Run File in Console", it'll execute your file exactly in a Python Console and you'll have an access to the interactive console after program execution.
Concerning 'runfile', no, unfortunately, it isn't possible to create a shortcut for a separate console command, but you can call it from Console's history.
For problem with opening new console for each run, I created an issue in our issue tracker: https://youtrack.jetbrains.com/issue/PY-28517
Feel free to follow it.
I found an kinda ugly solution to this problem:
In settings add the following code as “Starting script” (In settnings -> Build, Execution, Deployment -> Console -> Python Console:
This will create a variable work_dir. To make a short command to run this file again in the console you can create a makro.
(Edit -> Makros -> start macro recording) writing the following:
You can then later in settings -> keymap set a shortcmd to this makro.
It's not beautiful but it make pycharm run like spyder :)
Thanks for your input Niklasinde, but it only works on a very limited basis. First you need to be in the run window with your cursor and secondly you have to execute the same file again.
The tutorials on macros however brought me so far the best work around, it however still clutters the python console window with the contents of the whole file:
https://confluence.jetbrains.com/display/PYH/Using+macros
Hello Walinga and Shashkova,
I don't know if this has been picked up elsewhere, but I've also been trying to "Run File in Console" and expect it to behave as "Show command line afterwards" does in the community edition (or used to anyway), where you have access to the variables/classes created in the just-run script.
I think what needs to change is a default parameter to the runfile defined at /snap/pycharm-professional/[103,107]/helpers/pydev/_pydev_bundle/pydev_umd.py, line 135. Namely, the global_vars parameter should default to globals(), instead of None. Including that in the runfile(..., wdir) command that first executes would obviate the need for the macro.
edit: my setup may be part of it. I'm on ubuntu, trying to run through a docker container.
>I don't know if this has been picked up elsewhere, but I've also been trying to "Run File in Console" and expect it to behave as "Show command line afterwards" does in the community edition (or used to anyway), where you have access to the variables/classes created in the just-run script.
Are you referring to Python Console in PyCharm?
Hello Sergey, thank you for the quick response. I'm sorry for the vague note. I believe that this discussion is about being able to execute a python script (or module) *in the python console in PyCharm,* and then to continue within that console with access to variables/classes created in the just-run script. IDLE, Sypder, and (in terminal) python -i filename all do this, and without copying and pasting the script code in the console (Walinga's macro solution above), so it's not that unusual. However, PyCharm doesn't seem to update the globals and locals appropriately. In the below screenshot I run the quadratic_equation.py example from the docs, slightly modified to show globals and locals right before it quits:
"a" should be still be in scope after quadratic_equation.py is "run in console," but is not. More convenient behavior would be if the global_vars parameter to runfile were set as below:
There is already a working directory option in the Run Configuration, the text of which makes its way into the "wdir" parameter to the runfile command. You could have a checkbox to make explicit that the console should maintain the namespace of the script. As I said though, I don't see why "Run File in Console" doesn't already mean that, like "Show command line afterwards" did.
That's a known issue with remote interpreters I believe: https://youtrack.jetbrains.com/issue/PY-32819. Should be fixed in 2018.3.2.
Hello, I made something that seems easier for me rather than wasting time searching for the directories.
I just use CTRL + A (which selects all the code on the interpreter and then press ALT + SHIFT + E. Hope that this was helpful.
I'm using PyCharm 2020.1 on Windows. Moving over from spyder so I'm really used to running the entirety of the open module in an ipython console. Like others commenting here, the "Run File in Console" seems to behave for me exactly like the normal "Run".
CTRL+A followed by ALT+SHIFT+E works great, but it would be nice if it was possible to map a single keystroke (say, F5) to do exactly that.
@Sbs
It works fine in my environment.
How do you invoke Run File in Python Console?
When I run the python program it shows anamoly 64 bits why is this problem occurs
@...
Could you provide more information? Please copy-paste console output from PyCharm and indicate the issue.
Every time I Run File in Python Console, it runs the file in a new console. Is there any way to make it run in the existing console?
Edit: gotta love SO:
https://stackoverflow.com/questions/51831152/pycharm-running-python-file-always-opens-a-new-console