Run unsaved file in Editor without needing to save
See original GitHub issueIssue Report Checklist
- Searched the issues page for similar reports
- Read the relevant sections of the Spyder Troubleshooting Guide and followed its advice
- Reproduced the issue after updating with
conda update spyder
(orpip
, if not using Anaconda) - Could not reproduce inside
jupyter qtconsole
(if console-related) - Tried basic troubleshooting (if a bug/error)
- Restarted Spyder
- Reset preferences with
spyder --reset
- Reinstalled the latest version of Anaconda
- Tried the other applicable steps from the Troubleshooting Guide
- Completed the Problem Description, Steps to Reproduce and Version sections below
Problem Description
When attempting to run an unsaved file, the user is prompted to save the file. Is there a way to execute this code without needing to save the file? This can currently be done with Ctrl+A (select all) then F9 (Run selection or current line), but appropriately switching between this and the standard Run can be tedious.
Some possible solutions might be:
-
Execute code in IPython console: When an unsaved file is ran, contents are copied to the IPython console and executed. This might be the easiest approach; Issue https://github.com/spyder-ide/spyder/issues/5970 mentions recogizing unsaved files, and editor contents can already be executed in the IPython console using F9. I see this being an “execute unsaved files in the IPython console” option in the Editor Preferences.
-
Support for temporary files: When an unsaved file is ran, contents of the file are copied to a temporary Python file and executed. After execution, the temporary file is removed. This behavior would be similar to the Enthought Canopy Editor. It would be more work, but it would keep the IPython console clean.
Given the future support of the Language Server Protocol, I expect developers will want to use the Editor for small, multi-line code snippets more frequently than the IPython console. This would avoid the litter of untitled#.py files or unnecessary lines of code in the IPython console in the future.
What steps reproduce the problem?
- Create a new file in the Editor
- Make any changes to the new, unsaved file
- Press run (or F5) on that unsaved file
What is the expected output? What do you see instead?
As expected in the current version of Spyder, I am prompted to save the file before it can be executed.
Versions
- Spyder version: 4.0.0b1
- Python version: 2.7.13
- Qt version: 5.6.2
- PyQt version: 5.6
- Operating System name/version: Windows
Dependencies
pyflakes >=0.5.0 : 2.0.0 (OK)
pycodestyle >=2.3 : 2.3.1 (OK)
pygments >=2.0 : 2.2.0 (OK)
sphinx >=0.6.6 : 1.7.6 (OK)
rope >=0.9.4 : 0.10.7 (OK)
jedi >=0.11.0 : 0.12.1 (OK)
nbconvert >=4.0 : 5.3.1 (OK)
pandas >=0.13.1 : 0.20.1 (OK)
numpy >=1.7 : 1.12.1 (OK)
sympy >=0.7.3 : 1.0 (OK)
cython >=0.21 : 0.28.4 (OK)
qtconsole >=4.2.0 : 4.3.1 (OK)
IPython >=4.0;<6.0: 5.7.0 (OK)
matplotlib >=2.0.0: 2.0.2 (OK)
pylint >=0.25 : 1.9.2 (OK)
Issue Analytics
- State:
- Created 5 years ago
- Comments:7 (7 by maintainers)
Top GitHub Comments
I don’t think that the
Run File
command should do anything but run files. Files need to be saved and that’s usually a good thing.I have been in the situation where I’m learning a new package from demo code or testing code from stackedit where I am just learning/playing and it’s more convenient to change things in the editor and run it without saving because I would rather not keep it.
I’m personally good with solution 1, but it’s not a paste code and go solution you need to type
# %%
first. This leads us to solution 4(Which I could implement):Solution 4
Have the
Run Cell
command run all the text in the current editor tab when no code cells are present in the text.Currently the
Run Cell
command does nothing when a file no code cell comments. The work flow is simple:Run Cell
orctrl-enter
I personally find saving may work as a feature of F5, but I have had those “I just want to run the code. I don’t care what it’s saved as moments” so I do sympathize.
Solution 1: Use code cells instead of
F5
No Spyder coding required.
I think the best solution to using Spyder how you are describing is to use code cells. Just put a
# %%
at the start of your new file and copy in the code. The hot key for running the cell is justctrl-enter
even faster thanF5
. For super convenience you might even be able to add a# %%
to the end your new file template.The next solutions would need code changes in Spyder
Solution 2:
If you are still interested in running the file without saving, it would be possible to do it in a similar way to #7310 which is easier than messing with temp files.
Solution 3:
Interestingly when Spyder opens with a clean config it starts with a
temp.py
file which runs code without the save as window popping up, unlike the usual unnamed file .The temp file just says:
Once this temp file is closed the first time people can’t see how to get it back. One solution would be adding the option of
Open a new temporary file
to the file menu, but then even when the file is saved it will still be deleted when spyder is closed so I can see people not liking that very much.