I am learning about the kivy library for python. I have a test code which i run with C-u C-c C-c after having the python interpreter fired up with C-p. It runs fine the first time and the window opens, but trying to run it a second time makes nothing happen; the window doesn't show, only in the minibuffer below i see Sent: import kivy.

import kivy
from kivy.app import App
from kivy.uix.label import Label

class MyApp(App):
        def build(self):
                return Label(text = "Guido Van Rossum", font_size = 90)

def main():

if __name__ == "__main__":

Does anyone have experience with writing kivy using emacs, or can any one help?


This is what i get in the terminal the second time i run the code:

>>> [INFO              ] [Base        ] Start application main loop
[ERROR             ] [Base        ] No event listeners have been created
[ERROR             ] [Base        ] Application will leave
  • Does it work if you simply type main() or if you remove if __name__="__main__"? – Jules Aug 14 '16 at 20:47
  • (Edited - nothing happening is hardly the same as a "crash".) – Drew Aug 14 '16 at 20:50
  • @Jules No, no changes. – user12021 Aug 14 '16 at 21:03
  • This is not specific to kivy, but, in general, if you want to figure out what's going on inside your application, there are tools to do just that. For instance, there's a debugger, which is also possible to call from Emacs: stackoverflow.com/a/2325751/5691066 – wvxvw Aug 15 '16 at 5:38

I think this issue is caused by the fact that if you run the script with C-u C-c C-c and then run it a second time the same way, you are trying to run it as part of the same process/session (not sure the correct term). To illustrate this, if it needs illustrating, you can visit the *Python* buffer that will be open after you run the script, and at the interactive prompt and call the dir() function. You will see that 'App', 'Label', 'MyApp', 'kivy', etc. are already there.

For reasons that I can't really explain, but that have also been mentioned here, kivy will not work this way if you try to run the same App twice in the same session while everything is still in memory.

If you run your .py file from the command line with python myfile.py it will work every time you run it because you are starting from scratch each time. But if you open ipython or another interactive interpreter, import your module, and then call the main() function more than once, you will see the same errors for the same reason.

You will see that if you got to the *Python* buffer and type exit() to end the Python process, and run your script again with C-u C-c C-c, that it should work as expected again (although only once, of course, for the reasons described above). This is not an issue with Emacs really, but a product of the way that kivy works and the way the running Python scripts works in Emacs. I don't think it is something that you will easily be able to "fix", nor should you really need to. However, this will hopefully put your mind at ease if you were worried that it was a bug or something.

Emacs and kivy do play well together in my experience (I used them together every day at my last job), so I hope this little quirk doesn't bother you too much!

  • 1
    However in my experience i have always had some kind of problems with Emacs + Python, also i number of problems when writing Pygame, it mostly costs me to understand the problems why the code crashes in emacs and to fix it and get it to work properly than writing the program itself! For Python there are some other editors out there which really help one for writing python than get your head exploded because of some other stuff not related to the program. – user12021 Aug 15 '16 at 14:51
  • 1
    @amirteymuri I understand what you are saying, but in my experience, this is true for all editors. No editor is perfect, and if you are particular about how your editor works (which I am) you will experience some frustration when it doesn't work the way you want. The reason I use Emacs is that when something breaks or doesn't work the way I want, 99 out of 100 times I can a). fix it myself, b). get the answer from this great community. I personally can't do this with other editors. I guess what it comes down to is: use the tool that works for you. For me that is Emacs. – elethan Aug 15 '16 at 15:26
  • I use Emacs for writing c or clojure, but for python my experience have been till now DISASTER! May be you could give me some of your experience, how do you test or run parts or whole of your programs in emacs? Do you run them in an external terminal? – user12021 Aug 15 '16 at 17:02
  • 1
    @amirteymuri yeah, I almost always use an external terminal for testing and running code. I also, use anaconda-mode which I really like, and have used elpy in the past. Both of these packages make things a lot easier. I find anaconda-mode to be much simpler, and requires less tweaking for me. I don't really feel like Emacs is any more suited for writing Python than any other editor; it is really the non-Python packages and features that make me love Emacs, and I like having everything in one editor so I stick with it even when an editor built just for Python might do some things better. – elethan Aug 15 '16 at 17:20

I have the same problem, using kivy in Spyder editor. As mentioned in previous answers it happens for the way kivy works. But I found a work around for it here and here Which I copy paste below. Define the reset() function below, and simply run it BEFORE running your instance_of_App().run() method.

def reset():
import kivy.core.window as window
from kivy.base import EventLoop
if not EventLoop.event_listeners:
    from kivy.cache import Cache
    window.Window = window.core_select_lib('window', window.window_impl, True)
    for cat in Cache._categories:
        Cache._objects[cat] = {}

Your Answer

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