Spyder IDE with python 3.10 seems freezing when click run button, but it works fine if run a single line beforehand running the entire script

Question:

I have truble with last version of Spyder 5.4.0 with last version of Python 3.10.6.

  • Spyder version: 5.4.0 (conda)
  • Python version: 3.10.6 64-bit
  • Qt version: 5.15.2
  • PyQt5 version: 5.15.7
  • Operating System: Windows 10

Even if running a script like

print('Hello world')

when I click on the play green button, the IPython console seems freezing on this script, and it does not run for hours.
Spyder freeze when click run button

If I select this line of code and I run current selection or run the current cell it works fine. From this moment, it seems that spyder works fine, until at a certain point when I run it seems again freezing. I have to restart a new console and before running the script I have to run a single line or a single cell.

Spyder run button works only if I run a single line beforehand

It seems that I have to ‘activate’ in someway the Python console in order to Spyder will run the script.

Does anyone have the same issue? How can I solve it?

I have tried to uninstall and reinstall both spyder and python many times, but it is useless.

Answers:

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
Answered By: Marco Martino Rosso