Execute file in the console without opening a new one

Answered

Hello Everyone,

I just started using PyCharm and can't quite figure out how to implement my preferred set up. After some trying around and googling, I decided to ask for help here.

Coming from Sublime/Spyder/RStduio I am used to a framework where I have one interactive console (in PyCharm the "Python Console" with IPython is pretty much that thing) and all the code is run there.

So, Ideally, I have a shortcut for "run the file in the console" and it just does `execfile('filename.py')` in that one console.

Despite all my trials, PyCharm keeps creating new console instances (tabs) for every run.
My go to solution for now is "execute selection in the console" combined with "select all", but that's really not pretty.

Anything I am missing?

 

---

 

I understand why running the code in a clean new instance is better, but then I'd like to close all the old ones. Now if I am making small adjustments and re-run the file 5 times, I have to go and close all opened consoles manually...

 

Thanks!

0
8 comments
Avatar
Permanently deleted user

p.s. an article here suggests to set up a macro:

```
It would be nice if we could send the whole file to the console in one operation. Fortunately, we can record a macro. Select Edit→Macros→Start Macro Recording and perform the operations as before (select the whole file, send the selection to console, switch to the console). As you can see, my macro has only four actions (I press the down arrow key to remove the selection):
```

0
Avatar
Permanently deleted user

In Build, Execution, Deployment > Console, check off "Use existing console for "Run with Python console". This will re-use the same console.

4
Avatar
Permanently deleted user

How do I clear all variables between the runs if I am re-using the same console? Can this be done by default?

0

@Jetbrains

There is no such feature at the moment.

Please feel free to submit a feature request about it to our issue tracker using the link https://youtrack.jetbrains.com/issues/PY

0

I stumbled across a really evil hack to accomplish this. If you put parenthesis in the name of the run task, then it'll reuse the same tab each time you run that task. However the tab is restarted each time, so variables are cleared, and different run tasks get their own tab instead of a single shared one.

3

@Patrick: thx for that nice one, confirmed working here (with "my main()" as name of the run configuration) for:

PyCharm 2020.1.2 (Professional Edition)
Build #PY-201.7846.77, built on June 1, 2020
Runtime version: 11.0.7+10-b765.53 amd64
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o.
Linux 4.19.0-6-amd64
GC: ParNew, ConcurrentMarkSweep
Registry: debugger.watches.in.variables=false
Non-Bundled Plugins: com.intellij.plugins.netbeanskeymap, BashSupport, com.kite.intellij
Current Desktop: Undefined

0

@Jaleks

If you just want to run the file in the same Python Console each time, then enable Use existing console for "Run with Python Console in Preferences | Build, Execution, Deployment | Console

1

@Sergey Karpov: I've seen that comment above, but with that option PyCharm does not recognize any changed file as so, but runs the version which was current at first console usage over and over again. With using Patrick's hack, it works like expected, as long as that option is switched off: file changes are recognized, the console is re-used

0

Please sign in to leave a comment.