3

I'm trying to test a program written in Python with a front panel generated with Qt Designer (converted from .ui to .py using pyuic4). I can make the following code work outside Emacs, from a terminal (it is just a main window with some labels and buttons):

from PyQt4 import QtCore, QtGui
from panelSimple import Ui_VentanaPrincipal

class Window(QtGui.QMainWindow):
    def __init__(self):
        QtGui.QMainWindow.__init__(self)
        self.ui = Ui_VentanaPrincipal()
        self.ui.setupUi(self)

if __name__ == '__main__':

    import sys
    app = QtGui.QApplication(sys.argv)

    miVentana = Window()
    miVentana.show()

    sys.exit(app.exec_())

I would like to execute this code from within Emacs, but when I send the buffer to the Python interpreter, and it opens an "Inferior Python" buffer, nothing happens.

On the other hand, if I don't use any graphical user interface, python programs work very well.

Is it possible to make my program's GUI execute from inside Emacs?

I started using Spacemacs with its default settings. For my tries I select a virtual environment (with PyQt4 installed), and then I send the buffer to the interpreter. I don't know what other information could be useful to answer my question.

  • 1
    You didn't try reproducing what you've been doing outside Emacs inside it by using M-x shell and entering the same command? – wasamasa Jun 22 '15 at 20:58
  • I tried with M-x shell and it works. And I also tried deleting the "if name == 'main':" sentence (as @elethan suggest in his answer) and it works the way I wanted, inside Emacs. – El Diego Efe Jun 23 '15 at 13:14
  • If I print(__name__) before the if, it writes __main__. But then, nothing inside the if executes. Do you know why? – El Diego Efe Jun 23 '15 at 16:08
  • 1
    I'll answer myself: from python-shell-send-buffer (C-c C-c): With prefix ARG allow execution of code inside blocks delimited by "if __name__== '__main__':". So SPC u m s b in Spacemacs or C-u C-c C-c works. Thank you! – El Diego Efe Jun 23 '15 at 16:20
3

Try to use C-u C-c C-c. This allows you to run your script in an inferior Python buffer, and executes the code in the if __name__ == '__main__': block. I am not sure why, but when you simply send the script to the shell with C-c C-c without the prefix argument, it won't execute the code in the if __name__ == '__main__': block. This works for me at least, using the default Python mode from python.el in Emacs 24

  • 1
    At first C-u C-c C-c wasn't working for me because Spacemacs changes the argument (C-u) to (SPC u). After I realized it, it worked. The help from python-shell-send-buffer (C-c C-c) says explicitly: With prefix ARG allow execution of code inside blocks delimited by "if __name__== '__main__':". Thank you! – El Diego Efe Jun 23 '15 at 16:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.