2

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.

questionto42
  • 7,175
  • 4
  • 57
  • 90
  • I am using a bounty since it should be possible in PyCharm what is also offered in Spyder (using iPython console). I do not feel comfortable when working in PyCharm without having a pragmatic stop option. Imagine you have a long script, and you start something in the middle that runs far too long and you also do not want to restart the whole script again. Or you realise a small mistake or have a new idea during the running of a few lines, and you do not want to wait till it is over. In Spyder, I can simply stop, go on from the same point, and I need that in PyCharm as well. – questionto42 Sep 02 '20 at 20:40
  • 1
    News: there is no answer to this question, according to the PyCharm developers. No need for anyone else than PyCharm developers to answer here. There is already a feature request open in the PyCharm community. – questionto42 Sep 04 '20 at 19:44

3 Answers3

4

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

Andrey Resler
  • 301
  • 1
  • 4
  • 11
0

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

Andrey Resler
  • 301
  • 1
  • 4
  • 11
  • I have checked this, this is not what I need. I need to stop after using "Execute the Selection in the Python Console" which you start with [Alt+Shift+E]. And then I need to have access to the variables that are available till the last finished code line till then. The debug mode and pause button do not provide this. – questionto42 Sep 04 '20 at 06:32
0

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)?

Seth
  • 2,214
  • 1
  • 7
  • 21
  • It is not about pausing a script execution, but about stopping it, having the variables that were known till that point still in memory and *then* being able to go on from that memory point again. – questionto42 Sep 10 '20 at 10:44
  • @Lorenz That is what a breakpoint does. It stops execution, keeps all variables in memory, then gives the option to continue execution. – Seth Sep 10 '20 at 19:01
  • I do not see that, I do not have the flexbility then. A breakpoint only pauses the script, I want to stop, change code, go further with a new code selection which I determine only at that moment again, not before by setting a breakpoint. I simply need the stop option of an executed selection that is availbable in Spyder which is not available in PyCharm. Also: breakpoints are for the debug mode, not for the execution of a selection. – questionto42 Sep 10 '20 at 19:06