# Psychopy not quitting on core.quit() or sys.exit(0)

**URL:** <https://discourse.psychopy.org/t/psychopy-not-quitting-on-core-quit-or-sys-exit-0/933>\
**Category:** Coding\
**Tags:** spyder\
**Created:** [August 22, 2016, 10:46pm UTC](https://discourse.psychopy.org/t/psychopy-not-quitting-on-core-quit-or-sys-exit-0/933 "2016-08-22T22:46:45Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![tahabi](https://avatars.discourse-cdn.com/v4/letter/t/4af34b/32.png) [@tahabi](https://discourse.psychopy.org/u/tahabi)\
**Post date:** [August 22, 2016, 10:46pm UTC](https://discourse.psychopy.org/t/psychopy-not-quitting-on-core-quit-or-sys-exit-0/933/1 "2016-08-22T22:46:45Z")

</div>

I can’t seem to get the experiment window to close after the experiment has concluded using either core.quit() or sys.exit(). After the final screen has shown, the window simply hangs and in order to quit one needs to manually press the “stop” button in the IDE instead of having the application quit gracefully. I know the rest of the program isn’t hanging because it successfully prints verification output before it’s supposed to quit. Is there any better way to force the experiment window to close?

Thanks.

---

<div class="post-metadata">

**Author:** ![lindeloev](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/lindeloev/32/15_2.png) [@lindeloev](https://discourse.psychopy.org/u/lindeloev)\
**Post date:** [August 23, 2016, 7:53am UTC](https://discourse.psychopy.org/t/psychopy-not-quitting-on-core-quit-or-sys-exit-0/933/2 "2016-08-23T07:53:10Z")

</div>

What IDE are you using? Coder, Spyder, or something else? I had problems like this in Spyder when scripts were set to run in the ipython console. Ticking “run in new python process” (or something like that) solved the problem.

If you put a print statement after `core.quit()`, does it print? You could also try and run `win.close()`, although `core.quit()` should do that.

---

<div class="post-metadata">

**Author:** ![jon](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/jon/32/22_2.png) [@jon](https://discourse.psychopy.org/u/jon)\
**Post date:** [August 23, 2016, 10:02am UTC](https://discourse.psychopy.org/t/psychopy-not-quitting-on-core-quit-or-sys-exit-0/933/3 "2016-08-23T10:02:08Z")

</div>

Are you on a Mac, using one of the recent 64bit releases and using pyo (default sounds)? This is a known problem.

---

<div class="post-metadata">

**Author:** ![tahabi](https://avatars.discourse-cdn.com/v4/letter/t/4af34b/32.png) [@tahabi](https://discourse.psychopy.org/u/tahabi)\
**Post date:** [August 30, 2016, 6:55am UTC](https://discourse.psychopy.org/t/psychopy-not-quitting-on-core-quit-or-sys-exit-0/933/4 "2016-08-30T06:55:37Z")

</div>

Indeed I am. The experiment fails to quit on 1.84 on older 32bit Macs running older versions of OSX, as well. Perhaps I should manually specify the other sound library?

---

<div class="post-metadata">

**Author:** ![jon](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/jon/32/22_2.png) [@jon](https://discourse.psychopy.org/u/jon)\
**Post date:** [August 31, 2016, 7:28am UTC](https://discourse.psychopy.org/t/psychopy-not-quitting-on-core-quit-or-sys-exit-0/933/5 "2016-08-31T07:28:54Z")

</div>

yes that is exactly what you need to do

---

<div class="post-metadata">

**Author:** ![thomaswolf.cogsci](https://avatars.discourse-cdn.com/v4/letter/t/85e7bf/32.png) [@thomaswolf.cogsci](https://discourse.psychopy.org/u/thomaswolf.cogsci)\
**Post date:** [January 24, 2017, 2:44pm UTC](https://discourse.psychopy.org/t/psychopy-not-quitting-on-core-quit-or-sys-exit-0/933/6 "2017-01-24T14:44:47Z")

</div>

Hej tahabi and jon, I seem to have the same problem (psychopy not quitting and running a recent 64bit release and using pyo). To my knowledge, I had manually specified pyo with the commands:  
from psychopy import prefs  
prefs.general[‘audioLib’] = [‘pyo’]

But the problem is still there. tahabi, what do you mean with “the other sound library”? What am I missing here?

Thanks,  
Thomas

---

<div class="post-metadata">

**Author:** ![thomaswolf.cogsci](https://avatars.discourse-cdn.com/v4/letter/t/85e7bf/32.png) [@thomaswolf.cogsci](https://discourse.psychopy.org/u/thomaswolf.cogsci)\
**Post date:** [January 24, 2017, 2:51pm UTC](https://discourse.psychopy.org/t/psychopy-not-quitting-on-core-quit-or-sys-exit-0/933/7 "2017-01-24T14:51:50Z")

</div>

Sorry, guys… the problem appears to have been the following:

This order of imports didn’t work:  
from psychopy import prefs  
prefs.general[‘audioLib’] = [‘pyo’]  
from psychopy import core, visual, data, misc, event, logging, gui, sound,

This order of imports does work:  
from psychopy import core, visual, data, misc, event, logging, gui, sound, prefs  
prefs.general[‘audioLib’] = [‘pyo’]

This is probably completely obvious to you… to me it wasn’t.  
Thanks 😉

---

<div class="post-metadata">

**Author:** ![jon](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/jon/32/22_2.png) [@jon](https://discourse.psychopy.org/u/jon)\
**Post date:** [January 24, 2017, 8:56pm UTC](https://discourse.psychopy.org/t/psychopy-not-quitting-on-core-quit-or-sys-exit-0/933/8 "2017-01-24T20:56:21Z")

</div>

Actually @thomaswolf.cogsci I think the method you used in the first place is the one you want if your intention is to use `pyo`! I think what this suggests is that `pyo` is not working properly on your machine and causing it not to load.

If you go to the audio preference in psychopy (or print it from your script rather than setting) I think it will tell you that something else is being set by default (which is working) and that setting the preference to [‘pyo’] is causing the crash. The second code you’ve shown is (I think) setting the sound lib after it has already been initialised and has no effect.

All this will hopefully go away if we manage to find an alternative to pyo (I’ve added a new option recently that I’m hoping will be a good alternative)

---

<div class="post-metadata">

**Author:** ![thomaswolf.cogsci](https://avatars.discourse-cdn.com/v4/letter/t/85e7bf/32.png) [@thomaswolf.cogsci](https://discourse.psychopy.org/u/thomaswolf.cogsci)\
**Post date:** [January 27, 2017, 5:23pm UTC](https://discourse.psychopy.org/t/psychopy-not-quitting-on-core-quit-or-sys-exit-0/933/9 "2017-01-27T17:23:50Z")

</div>

hmm… I manually switched the order in the settings from [‘pygame’, ‘pyo’] to [‘pyo’, ‘pygame’] and exiting works again.  
most of the time… I will post again when I have more conclusive experience with what’s going on.

---

<div class="post-metadata">

**Author:** ![thomaswolf.cogsci](https://avatars.discourse-cdn.com/v4/letter/t/85e7bf/32.png) [@thomaswolf.cogsci](https://discourse.psychopy.org/u/thomaswolf.cogsci)\
**Post date:** [January 29, 2017, 11:57pm UTC](https://discourse.psychopy.org/t/psychopy-not-quitting-on-core-quit-or-sys-exit-0/933/10 "2017-01-29T23:57:36Z")

</div>

My newest hypothesis is that the freezing occurs, because of an issue with pyo server. In my case (I want to use the sound module and the voicekey module) the experiment crashes when voicekey tries to initialize a pyo server even though the sound module has already initialized one.  
I have learned that you cannot have two pyo servers at the same time. The solution seems to be to share the one server created by the sound module with the voicekey module. I found a reference to this in a discussion on github:

[https://github.com/psychopy/psychopy/issues/1282](https://github.com/psychopy/psychopy/issues/1282)

My only remaining question: how do I link sound and voicekey to the same server?

Thanks,  
Thomas

---

<div class="post-metadata">

**Author:** ![hubertv](https://avatars.discourse-cdn.com/v4/letter/h/e8c25b/32.png) [@hubertv](https://discourse.psychopy.org/u/hubertv)\
**Post date:** [June 8, 2017, 12:07pm UTC](https://discourse.psychopy.org/t/psychopy-not-quitting-on-core-quit-or-sys-exit-0/933/11 "2017-06-08T12:07:53Z")

</div>

The sound module should be imported sooner.

we changed the generated import statements:

from psychopy import locale\_setup, visual, core, data, event, logging, sound, gui

to:  
from psychopy import sound  
from psychopy import locale\_setup, visual, core, data, event, logging, gui

and core.quit() no longer freezes.

@jon: can you change the Psychopy Builder script compiler so that it imports the sound module earlier? It might be a good idea to also import the visual module earlier.

Best,  
Hubert
