question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Run unsaved file in Editor without needing to save

See original GitHub issue

Issue 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 (or pip, 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?

  1. Create a new file in the Editor
  2. Make any changes to the new, unsaved file
  3. 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:closed
  • Created 5 years ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
bcolsencommented, Aug 26, 2018

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:

  1. Copy demo code
  2. Paste into new file
  3. Tweak values (optional)
  4. Run Cell or ctrl-enter
1reaction
bcolsencommented, Aug 24, 2018

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 just ctrl-enter even faster than F5. 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:

# -*- coding: utf-8 -*-
"""
Spyder Editor

This is a temporary script file.
"""

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How can I run an unsaved file? - notepad++ - Super User
I sometimes just want to see the output of some small scribbles without having to manually save them in a temp dir or...
Read more >
Running code without saving - Google Groups
Sometimes I want to just write out a few lines of code and execute them, without saving them to a file. Spyder insists...
Read more >
Sublime Text: run code in a new file without saving to disk and ...
I installed SublimeREPL, how to run unsaved code ? · Open a REPL, then switch to your code, select it, and click Tools...
Read more >
Issue 19042: Idle: run from editor without explicit save
Idle allows only one edit window per disk file (path) but allows multiple Untitled windows not associated with any path. The 'unsaved' flag ......
Read more >
Save and revert changes | JetBrains Rider Documentation
You can mark unsaved files with an asterisk (*) on the file tab: In the Settings/Preferences dialog ( Ctrl+Alt+S ), select Editor |...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found