When using IPython, I try and type in a multi-line code snippet using the %edit "magic function". I'm using the default settings for IPython and I'm on Windows 7, so this brings up Notepad. However, when I try to save the file when I'm done typing, it tells me that it can't save the file because it is in use by another process. Checking using LockHunter, it's the same Python process that created the file (i.e. IPython). I've been able to unlock the file with LockHunter as well, but I can't keep doing this with every temporary file. How can I get IPython to stop locking its temp files created by %edit ?
Asked
Active
Viewed 223 times
2
-
It looks like IPython creates temporary files in a way that opens them, and then doesn't close them before starting the editor (only Windows has a problem with that). Do you want to file an issue, or better yet, make a pull request? – Thomas K Apr 09 '14 at 00:41
-
@ThomasK, it uses [`tempfile.mkstemp`](https://docs.python.org/3/library/tempfile.html#tempfile.mkstemp) in [`InteractiveShell.mktempfile`](https://github.com/ipython/ipython/blob/rel-2.0.0/IPython/core/interactiveshell.py#L3023). Some editors on Windows will work since the file can still be opened and written. But Windows won't let the file be deleted, replaced, or renamed. Notepad doesn't keep the file handle open, so saving probably tries to replace the file on disk with a new file, which fails. – Eryk Sun Apr 09 '14 at 04:03
-
Monkeypatching `mktempile` to call `os.close(handle)` 'solves' the problem, but hopefully there's a more secure solution. – Eryk Sun Apr 09 '14 at 04:04
-
Do you want to make that a real patch, rather than a monkeypatch? It seems like the obvious way to fix it. – Thomas K Apr 10 '14 at 19:24