Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added option to use magic in .py files #280

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

pinguicula
Copy link

No description provided.

@ccordoba12
Copy link
Member

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

@pinguicula
Copy link
Author

pinguicula commented Mar 9, 2021

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.

@ccordoba12
Copy link
Member

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?

@impact27
Copy link
Contributor

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?

@pinguicula
Copy link
Author

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

@impact27
Copy link
Contributor

impact27 commented Feb 14, 2023

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

@pinguicula
Copy link
Author

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

@impact27
Copy link
Contributor

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

@pinguicula
Copy link
Author

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.

@impact27
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants