1

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?

gnirx
  • 197
  • 1
  • 1
  • 8

2 Answers2

2

python-mode.el comes with a command py-execute-buffer-dedicated, opening a new and reserved process for it

Andreas Röhler
  • 4,804
  • 14
  • 18
1

In python.el, a new inferior process is launched in a new buffer if the python-buffer variable is set to nil. Therefore, it's possible to advise the python-send-buffer function to reset that variable to nil after every invocation, thereby forcing a new Python process to be executed for every subsequent python-send-buffer command. Something like the following should work:

(defadvice
  python-send-buffer
  (after python-send-buffer-new-proc activate)
  (setq python-buffer nil))

(ad-activate python-send-buffer)

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 if python-mode.el doesn't use a similar mechanism. If I have time, I'll try to look into it.

Edit: the python-mode.el package uses the command py-shell to 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-mode isn'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 multiple imports of the same module being the issue, as Python modules are only loaded once, with successive import statements having no effect (for that, an explicit reload or execfile() is required). If I'm completely off-base here, I apologize, but I felt this possibility might merit mention.

Greg E.
  • 2,722
  • 1
  • 16
  • 22
  • Thanks, that helped a lot. To the Twisted problem: I do an `from twisted.internet import gtk3reactor gtk3reactor.install()` as per Twisted manual which causes the problem when being evaluated a second time. Is there a standard way of checking whether a module has been imported before? – gnirx Jun 21 '12 at 11:52
  • @gnirx, normally, importing a module a second time has no effect, and if it does have some effect, particularly an error-producing one, it's probably an indication that the code should be restructured in some way. That said, maybe your case is an exception -- I don't know enough about the particulars of Twisted to be certain either way. If you really want to check whether a module has been imported, `import sys` and check whether it's present in `sys.modules`. Keep in mind, that's generally considered kludgy, and it'd be better to avoid if possible. – Greg E. Jun 21 '12 at 19:53