1

My task is to repeatedly rerun a code from beginning. As I do not want to run the code to the end I set a breakpoint() command. However, Spyder freezes when the code is the second time called from debug mode. For demonstration a simple program is sufficient:

print('Hello World!')
breakpoint()

After launching Spyder I start the small program by F5. Everything works as expected and I jumped into the debug mode. Now I press again F5 and the code restarts correctly from the debug mode. After again pressing F5 the code is not restarted and instead the IDE freezes. It is not possible by any means to rerun the code again from debug mode. Interrupting the kernel by pressing the red button (see screenshot below) does not help. Trying to leave the debug mode by !q does not leave the debug mode. The only possibility to rerun the code is quite cumbersome, i.e. I have to restart the kernel (Ctrl+.) that takes a while. How to debug code under these circumstances?

Is this a bug of the IDE or expected behavior? How can I restart the code by a simple key press as often as possible from debug mode?

This is a screenshot of the Spyder console with comments in red added. enter image description here

I use a recent version a WinPython with latest Spyder 5.4.0, Python 3.10.4 64-bit, Qt 5.15.2, PyQt5 5.15.6, Windows 10Pro Version 21H2. The problem also appears with Spyder 5.3.3 A similar problem (or maybe the same) appears with a different Python version.

  • I noticed that if I restart kernel only, it hangs when I click on play button, however if I clear all variables for all the users appartently it works fine for at least one python script run. Did you noticed also this kind of behaviour? it is quite strange that no other users had this problem, I can't figure out any solution on the web, it is very strange – Marco Martino Rosso Nov 29 '22 at 09:37
  • (*Spyder maintainer here*) You're running a file inside the debugger that has a `breakpoint` call in it, which is used to start a debugging session. In other words, your code is trying to start the debugger when you're already inside it, so I'm not surprised you're getting this kind of problem. My recommendation is for you to stop the debugger once you're done debugging and start it again to avoid this; or to comment the `breakpoint` call once you're inside the debugger so that additional file runs don't see it. – Carlos Cordoba Dec 12 '22 at 02:34
  • @CarlosCordoba Your comments do not explain why only the 2nd call from the debug mode freezes but not the first call from debug mode. My aim is to repeatedly run a code from the beginning and interrupt it a given point. This is a standard procedure and I want to do this by a single key press. Your comments show that this is not possible and cumbersome handling is necessary. Actually I do not need the debug mode. The only thing I need is that the program runs from the beginning, stops at a defined position where I can access my variables. How can I do this by single key press? – granular bastard Dec 14 '22 at 11:43

1 Answers1

0

After many trials, I noticed that there is something strange with IPython console. I noticed that when it hangs after running a code, if I delete all user variables, it worked fine.

Then I tryed to delete all variables before execution, and it work fine.

Therefore I discovered that a solution that worked for me is to go to preferences -> Run -> and untick the option 'Remove all variables before execution'

It is quite annoying because I have to do it manually every time before running, but in this way the spyder does not appear to hang anymore! I hope that the Spyder developers will solve it soon.

-

I automatically solved by typing at the beginning of any script these lines, inspired from the question Code to clear console and variables in Spyder :

try:
    from IPython import get_ipython
    get_ipython().magic('clear')
    get_ipython().magic('reset -f')
    import matplotlib.pyplot as plt
    plt.close('all')
except:
    pass

similar to Matlab in which you normally start your code with

clc
close all
clear all