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

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

4 Likes

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.

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.

2 Likes

The fix that @bjoern describes works beautifully. I am so happy to have found this. Yo have saved my day, thank you so so much.

Not sure why you actually didn’t follow the instructions in the OP.

import pyglet
pyglet.options['shadow_window']=False

at the very beginning of your script also fixes the _isFullScr issue for me.

Meanwhile I gave a check to 1.85.6 too, and it doesn’t seem anything of what you did ever solved a thing @jon

That is entirely possible. I don’t have any system that actually exhibits the problem (it requires a computer with broken drivers) which means I can’t test whether solutions actually work. If you have a system that exhibits the problem then it would be great if you’d submit a pull request with the corrected fix. That’s the benefit of a community (open-source) project. :slight_smile:

Thanks

AFAIU (also noted in the sourcecode) the problem is that options have to be set before importing pyglet.gl or pyglet.window.

So, I dunno, if it is possible to have the except pyglet.gl.ContextException reinitialize like everything.
…if in lack of alternatives though, I guess throwing a warning for the user to try using shadow_window himself would be at least better than what we have now (maybe only if pyglet.gl.gl_info.get_vendor returns Intel?)

p.s. I tried to nag upstream developer to check whether this (G33 graphics on XP) is a bugged driver, or just an overzealous one

How do you make the changes to the window.py file? When I tried to make these edits, it is unable to save and says “Error: Access is denied.”

It sounds like you may have to open your text editor with administrator rights before editing that file. If you are unsure how to do that, just do a web search for “Run as administrator” and your respective windows version.

I missed this comment when it was originally posted. The reason I made the changes directly to the window.py file is because we have PsychoPy installed on a whole bunch of laptops of that particular model, and I wanted to fix the issue in such a way that our users would not have to deal with it each time they created a new experiment.

What hardware/drivers/OS do you have btw? (also @bsobel)

I tried some old potatoes I had around at home, but I couldn’t reproduce the issue anymore (thus try to help the pyglet guys).