spyder-ide / spyder

Official repository for Spyder - The Scientific Python Development Environment
https://www.spyder-ide.org
MIT License
8.33k stars 1.62k forks source link

Running a cell instead runs a whole file #14173

Open shivendra90 opened 4 years ago

shivendra90 commented 4 years ago

Description

I started Spyder to do work further on one of my academic assignments. For some reason which I think could be a bug, pressing Ctrl+Enter (run a cell) ran the whole file instead. This is the first time I'm observing this. I've been creating cells in the python script I'm writing for the last couple of days and cells ran fine. Doing a restart of Spyder solves this problem.

What steps will reproduce the problem?

Steps to reproduce:

  1. Launch Spyder.
  2. Create some cells (#%%).
  3. Write some code in it and try running the cells one by one.

In most cases the cells will run fine but it could be that you accidentally run the whole file instead when running any of the cell. I do not have a snapshot for this or I could have shown it here, so apologies if I'm not able to show any evidence. I did a restart and now the cells run fine.

Versions

Dependencies


# Mandatory:
atomicwrites >=1.2.0           :  1.4.0 (OK)
chardet >=2.0.0                :  3.0.4 (OK)
cloudpickle >=0.5.0            :  1.6.0 (OK)
diff_match_patch >=20181111    :  20200713 (OK)
intervaltree                   :  None (OK)
IPython >=4.0                  :  7.18.1 (OK)
jedi =0.17.1                   :  0.17.1 (OK)
keyring                        :  None (OK)
nbconvert >=4.0                :  6.0.7 (OK)
numpydoc >=0.6.0               :  1.1.0 (OK)
parso =0.7.0                   :  0.7.0 (OK)
pexpect >=4.4.0                :  4.8.0 (OK)
pickleshare >=0.4              :  0.7.5 (OK)
psutil >=5.3                   :  5.7.2 (OK)
pygments >=2.0                 :  2.7.1 (OK)
pylint >=1.0                   :  2.4.3 (OK)
pyls >=0.34.0;<1.0.0           :  0.35.1 (OK)
qdarkstyle >=2.8               :  2.8.1 (OK)
qtawesome >=0.5.7              :  1.0.1 (OK)
qtconsole >=4.6.0              :  4.7.7 (OK)
qtpy >=1.5.0                   :  1.9.0 (OK)
rtree >=0.8.3                  :  0.9.4 (OK)
sphinx >=0.6.6                 :  3.2.1 (OK)
spyder_kernels >=1.9.4;<1.10.0 :  1.9.4 (OK)
watchdog                       :  None (OK)
xdg >=0.26                     :  0.26 (OK)
zmq >=17                       :  19.0.2 (OK)

# Optional:
cython >=0.21                  :  0.29.21 (OK)
matplotlib >=2.0.0             :  3.3.1 (OK)
numpy >=1.7                    :  1.19.1 (OK)
pandas >=0.13.1                :  1.1.3 (OK)
scipy >=0.17.0                 :  1.5.2 (OK)
sympy >=0.7.3                  :  1.6.2 (OK)
steff456 commented 4 years ago

Hi @shivendra90,

So, you will like to have a checkpoint of the state of the console in case you accidentally press run file?

shivendra90 commented 4 years ago

Sorry I did not get that @steff456 . You mean you would like to see some log for the console when this occurred? I actually did not press 'run file' since the script is long and I personally prefer making cells and running them instead of running each line.

steff456 commented 4 years ago

I think I'm not getting what error you are experiencing. Do you have any reproducible case we can use to try to get the same behavior you are having?

shivendra90 commented 4 years ago

There is no error message per se. It's just when yesterday I was trying to run some cells from the python script in Spyder, instead the whole file was being executed when pressing Ctrl+Enter or hitting the 'run cell' button on the toolbar. That was not what I was expecting. I certainly thought this could be a bug. I'm not sure if anyone else has experienced this. For example if I have a cell like this:

#%% Load libraries
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sb

from statsmodels.formula.api import ols
from statsmodels.stats.anova import anova_lm
from pandas import read_csv, DataFrame

When running this cell essentially there should be no output. However, when I ran this the whole python script was being executed as if I had hit F5 and that of course took a lot of memory. I thought I might have hit the wrong keys but that was all correct; I had to restart Syder to get individual cells working again. i hope you get my point. I think I'm the only one who has seen this.

steff456 commented 4 years ago

Did you have more cells after the one you are showing me?

shivendra90 commented 4 years ago

Yes. The python script has 11 complete cells. Upon running any of the cells, for some reason the whole file was being run. This is the history of the console that I'm pasting here:

runcell("Load libraries")
runcell('Load libraries', '/home/shivendra/Desktop/Great Learning/pgp dsba/Advanced Stats/Final project/adv_stats.py')
runcell('Load libraries', '/home/shivendra/Desktop/Great Learning/pgp dsba/Advanced Stats/Final project/adv_stats.ipy')
runcell('Load libraries', '/home/shivendra/Desktop/Great Learning/pgp dsba/Advanced Stats/Final project/adv_stats.py')
runcell("Load libraries")

I don't think so this will be very helpful but you can notice that I tried running the same cell over and over again. This is because the cell 'Load libraries' was instead running the whole file and I thought I might be hitting the wrong keys. I also tried running it manually by typing in the command runcell() but it still ran the whole file. I tried this for several minutes and eventually did a restart of Spyder. However my time was up so I decided to check on this again yesterday where I paste the history:

runcell("Load libraries")
runcell("Load data and display view")
runcell('Describe data and info', '/home/shivendra/Desktop/Great Learning/pgp dsba/Advanced Stats/Final project/adv_stats.py')

Now you can notice that I'm running each cell one by one and the output came as expected. Overall it was not a breaking problem, a simple restart fixed it but certainly something was not right and thought I might report here.

steff456 commented 4 years ago

Ok! Please if you see that you can reproduce this error in the future, and can give us the code you are working in please add it here in this issue so we can try to reproduce it. I will leave this issue open if someone else encounters the same error and we can track it down 😊

joooeey commented 1 year ago

This bug is still present in Spyder 5.3.3 and like the OP I can resolve it with a restart of Spyder.

For me it shows up when renaming a file and the target name has the suffix .ipy. As in:

One way to reproduce:

  1. Create a new Spyder project containing the file temp.ipy with the following content:
# %% a
print("a")

# %% b
print("b")
  1. Place the cursor in the file and press Ctrl+Return. This prints either "a" or "b" depending on which cell the cursor is in.
  2. In the "Project" pane, rename the file to newname.ipy.
  3. Repeat step 2. Now this prints both "a" and "b" on separate lines.
  4. Restart Spyder.
  5. Repeat step 2. Again only one cell is run, as expected.

Versions

ccordoba12 commented 1 year ago

Hey @joooeey, thanks for the detailed description on how to reproduce this problem. We'll take a look at it for Spyder 6, to be released later this year.

PeterHristov commented 3 weeks ago

Just wondering if there is any progress on this. Using Spyder 6.0.1, Python 3.11.4 64-bit, Qt 5.15.2, PyQt5 5.15.11, still exhibits the issue. On top of this pressing F9 which is supposed to 'Run selection or current line' gives the same result. Thanks!

ccordoba12 commented 3 weeks ago

Using Spyder 6.0.1, Python 3.11.4 64-bit, Qt 5.15.2, PyQt5 5.15.11, still exhibits the issue.

What's exactly the issue @PeterHristov?

On top of this pressing F9 which is supposed to 'Run selection or current line' gives the same result.

What's the result? Could you post a video showing that?

PeterHristov commented 1 week ago

@ccordoba12, the issue is the one mentioned by the OP - Ctrl+Enter runs the whole file and so does F9. It persisted for a while, but I haven't encoutered it since my post. I use Spyder on an almost daily basis, so I will be on the look out and if I can capture the behaviour I will post about it here.

ccordoba12 commented 1 week ago

the issue is the one mentioned by the OP - Ctrl+Enter runs the whole file and so does F9.

Ctrl+Enter does run the entire file if no cell is present. However, F9 shouldn't do that.

I use Spyder on an almost daily basis, so I will be on the look out and if I can capture the behaviour I will post about it here.

Ok, thanks for your help with that.

rapper2000 commented 6 days ago

Is there any progress on this problem? because i am experiencing it a lot in a large project and it really anoys me.

edit:

Did you change the standard Hotkeys for spyder 6.0 onwards? Because i found that STRG+Enter is now "execute cell" per default, whereas Shift+Enter is now "Execute cell and advance". I am a longtime user, and this behavior seems odd to me, especially since i never changed or change the hotkeys

found here