spyder-kernels icon indicating copy to clipboard operation
spyder-kernels copied to clipboard

Added option to use magic in .py files

Open pinguicula opened this issue 4 years ago • 10 comments

pinguicula avatar Mar 09 '21 10:03 pinguicula

Hey @pinguicula, thanks for your contribution. Could you explain us what you're trying to achieve with these changes?

ccordoba12 avatar Mar 09 '21 14:03 ccordoba12

Hi @ccordoba12 and thanks for your quick answer !

Sometime when I debug .py files I want to be able to use magic and flexible indentation. With this change, the user have the option to allow .py-files to be treated in the same way as .ipy files. (When I say debug, I mean that I run the code cell by cell in spyder).

I appreciate your motivation that all .py files should be able to execute outside of spyder but this gives the user the option (note, completely optional) to debug .py files as if they were .ipy. My current change is not very intrusive but I am thinking that this could possibly be changed to something in the GUI: a checkbox or list of file extensions that are treated as .py.

Thanks for considering this.

pinguicula avatar Mar 09 '21 15:03 pinguicula

My current change is not very intrusive but I am thinking that this could possibly be changed to something in the GUI: a checkbox or list of file extensions that are treated as .py.

Yep, I was thinking exactly that because perhaps only you and us would be the only ones aware of this possibility.

@impact27, what do you think about this?

ccordoba12 avatar Mar 09 '21 16:03 ccordoba12

I think magic are only valid in .ipy files, because if you use magics in your file, then it is not a python file, but a ipython file. You can just rename your file (file.py -> file.ipy), unless you have a usecase where this is not possible?

impact27 avatar Feb 12 '23 09:02 impact27

I am using spyder in my workflow for development of .py files. I use cells very frequently. Running cells are interpreted as flexible indentation which means spyder think that they are magic. Example:

for a in b:
#%% <- This cell is sometimes, but not always, not possible to run due to the flexible indentation.
    x = fun()

pinguicula avatar Feb 13 '23 20:02 pinguicula

I am not sure I follow:

b = [1, 2]
for a in b:
#%%
    print(a)

runs fine for me both in spyder and with python3.

#%% is a comment, not a magic, that spyder interprets as a cell break. This works both for .py and .ipy files.

A magic would be for example: %matplotlib Which wouldn't run in python, so shouldn't be in a .py file

impact27 avatar Feb 14 '23 06:02 impact27

Cells are sometimes interpreted as flexible indentation which spyder thinks are magic.

pinguicula avatar Feb 14 '23 14:02 pinguicula

Do you have an example of code that would do that?

impact27 avatar Feb 14 '23 15:02 impact27

I am afraid not. I think it is a bug and it can appear suddenly and then disappear again after restarting the kernel.

In any case, since I am using an IPython interpreter while I am in spyder, I would like to be able to use magic. Personally, patching spyder is sufficient, but I am sure that I am not the only one with this setup and issue.

pinguicula avatar Feb 14 '23 18:02 pinguicula

If you can reproduce I would be interested to see the code that triggers this behaviour.

Usually I would just use the console to write magics. I never had a problem running cells separated by #%% even while debugging.

impact27 avatar Feb 14 '23 19:02 impact27