I have a small GTK python application that imports a package (Twisted) that may not be loaded twice.
If I run my application in emacs with python-mode.el and press C-c C-c, the application gets executed in a python shell window.
If I now close the application, the python shell stays up and running. If I now press C-c C-c again, emacs “reuses” the old python process and thus I run into problems because I’m installing a Twisted reactor twice.
Is it possible to have python-mode.el open a new shell window each time I execute a buffer?
In
python.el, a new inferior process is launched in a new buffer if thepython-buffervariable is set tonil. Therefore, it’s possible to advise thepython-send-bufferfunction to reset that variable tonilafter every invocation, thereby forcing a new Python process to be executed for every subsequentpython-send-buffercommand. Something like the following should work:I know that your post was asking for help with
python-mode.el, but I thought it might be helpful to mention this anyway, as I’d surprised ifpython-mode.eldoesn’t use a similar mechanism. If I have time, I’ll try to look into it.Edit: the
python-mode.elpackage uses the commandpy-shellto initiate a new inferior Python process. I found a mailing list posting in which a user provides an ad hoc function that appears to do what you need.By the way, it might be worth considering that trying to alter the default behavior of
python-modeisn’t the best approach to this problem. I don’t know what your code does, and I’m not particularly familiar with Twisted, but it seems to me that experiencing major errors when evaluating your code a second time within the same session could be a sign of a more fundamental design problem. I fail to see how it could be a matter of multipleimportsof the same module being the issue, as Python modules are only loaded once, with successiveimportstatements having no effect (for that, an explicitreloadorexecfile()is required). If I’m completely off-base here, I apologize, but I felt this possibility might merit mention.