psychopy.org | Reference | Downloads | Github

Bugfixes for "Unable to share contexts" and "Portaudio not initialized"


#1

I’ve spent much of this semester struggling to make a working Word Pronunciation Task. Along the way there have been some struggles with getting PsychoPy to run right on older (2009) computers.

BUG: "pyglet.gl.ContextException: Unable to share contexts […] “‘Window’ object has no attribute ‘useNativeGamma’”

SOLUTION: Add two lines to the top of the .py file, as explained here.

from __future__ import absolute_import, division
# Fix pyglet 'shared environment' error
import pyglet
pyglet.options['shadow_window']=False
# script continues as normal

BUG: “Portaudio not initialized” leads to crashes when using a microphone.

SOLUTION: Open Windows’ “Sound” panel, open the properties of your playback device, go to the advanced tab, and make sure that the default format is set to 44100Hz instead of 48000 Hz. As explained here.

For more details, see


#2

Could you help me test a possible fix for the pyglet window issue? Could you find the window.py file in your psychopy installation and find the line where the window is created, around line 1499. Try replacing it with this try...except code:

        try:
            self.winHandle = pyglet.window.Window(
                    width=w, height=h,
                    caption="PsychoPy",
                    fullscreen=self._isFullScr,
                    config=config,
                    screen=thisScreen,
                    style=style)
        except pyglet.gl.ContextException:
            # turn off the shadow window an try again
            pyglet.options['shadow_window']=False
            self.winHandle = pyglet.window.Window(
                    width=w, height=h,
                    caption="PsychoPy",
                    fullscreen=self._isFullScr,
                    config=config,
                    screen=thisScreen,
                    style=style)
            logging.warning("Pyglet shadow_window has been turned off. This is "
                            "only an issue for you if you need multiple "
                            "stimulus windows, in which case update your "
                            "graphics card and/or graphics drivers.")

Hopefully then you won’t need to tweak the code from your experiments manually in future!

The idea is that we’ll try to create the window once, and if it fails automatically try again with shadow window turned off. But I don’t know if pyglet will allow us two bites at the cherry, and I can’t test it myself.


#3

I ran into this issue with PsychoPy 1.85.6 on some older Win XP laptops, so unfortunately those code changes, which are already included in 1.85.6 (via this commit), don’t seem to work.

It turns out, that even setting pyglet.options['shadow_window']=False at the very beginning of the try block does not prevent the “pyglet.gl.ContextException: Unable to share contexts” error. Furthermore, it even looks like that try block is never actually executed, because if you put logging.warning("Beginning of try block") at the very beginning of that try block, that message never makes it to the log.

Since I needed a quick solution, I have now put the pyglet.options['shadow_window']=False statement directly under import pyglet at the beginning of window.py (in C:\Programs\PsychoPy2\Lib\site-packages\psychopy\visual). That does fix the issue and makes PsychoPy usable again on those affected machines for our users.

In PsychoPy 1.9, window.py seems to have gotten rewritten substantially, so I am not sure how to apply this fix there. The error message is different there, too (“AttributeError: ‘PygletBackend’ object has no attribute ‘_isFullScr’”).

But if someone else needs to get PsychoPy 1.8x Builder experiments working on old PCs that produce "pyglet.gl.ContextException: Unable to share contexts // 'Window' object has no attribute 'useNativeGamma'" errors, you can patch your PsychoPy install the way I have described above to get it working again. I hope this helps others as much as this thread has helped me.