questionto42
questionto42

Reputation: 9610

PyCharm (i)Python Console: "Stop current command" of "Execute Selection in Python Console" (=Alt+Shift+E) process without losing variables in memory?

I am used to being able to force-stop the running of a selection of code. This is possible in vscode and Spyder without losing the variables in memory.

Example from Spyder, which is using iPypthon console: you run a command with the same "Run selection or current line" (in Spyder: F9) process, you realise that it takes too long and you want to stop it. You simply use "Stop current command" (red square).

enter image description here

After the stop, the memory is not lost, all variables are still in memory that have been known at the stop moment. Proof:

enter image description here

When you go on in Spyder, you can choose whatever code line that has been passed till the stop moment to go on with (you can also choose later code lines, but that usually makes no sense). For example, if you stop a long running loop, change the code of that loop and then run it again, or you go back 10 code lines to start from there instead.

You are simply free to go on from any code line to execute a new selection.

In Contrast, in PyCharm, I only find the red square "Stop Console" which stops the console completely, no action possible after this, all variables in memory are lost.

After pressing the red square or pressing Ctrl+C in the "Python Console" the console is closed.

enter image description here

The console prints

Process finished with exit code 0

and no input is possible anymore. After this stop, there is also no option anymore to "Execute Selection in Python Console" (Shift+Alt+E) either, it leads to:

enter image description here

"Rerun" / "New Console" both restart the kernel, the variables in memory are lost:

enter image description here

Using iPython console instead of the standard Python console (you can install this under [File>Settings>MyProject>Project Interpreter], see How To Add The IPython Console To PyCharm), same issue:

enter image description here

And this happens even though in Spyder, which uses iPython, a "stop" puts me back to the iPython shell without losing the variables in memory. I guess that this thread Can I stop execution of current module in ipython without leaving ipython is about a similar problem, though not paticularly related to PyCharm.

How can I get a stop option in PyCharm that is similar to the "Stop current command" option of Spyder so that the "stop" keeps the variables in memory and I can go on from where it has stopped?

Mind that the thread Keyboard interrupt in debug mode PyCharm is not a duplicate of this, since its accepted answer leads to a closed kernel as well.

Upvotes: 4

Views: 7837

Answers (3)

Andrey Resler
Andrey Resler

Reputation: 311

While it is possible in PyCharm to pause the script when running via the run/debug configuration, unfortunately the "pause" action is not available when running a selection in console, neither is the "stop" button you've requested. The current implementation of the "stop" button sends SIGTERM/SIGKILL.

A feature request has been created for this issue: https://youtrack.jetbrains.com/issue/PY-44346

Upvotes: 5

Seth
Seth

Reputation: 2369

The only option here would be to use breakpoints. You can add them by clicking the area to the right of the line number.

Credit: How to pause script execution in PyCharm Community?

How to pause program execution in Pycharm (pause button not working)?

Upvotes: 0

Andrey Resler
Andrey Resler

Reputation: 311

Debugger has the pause action, which can be accessed from the toolbar, or from the menu.

https://www.jetbrains.com/help/pycharm/pausing-and-resuming-the-debugger-session.html

enter image description here

enter image description here

Upvotes: 0

Related Questions