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

**URL:** <https://discourse.psychopy.org/t/bugfixes-for-unable-to-share-contexts-and-portaudio-not-initialized/3537>\
**Category:** Coding\
**Created:** [December 1, 2017, 9:23pm UTC](https://discourse.psychopy.org/t/bugfixes-for-unable-to-share-contexts-and-portaudio-not-initialized/3537 "2017-12-01T21:23:51Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Joe-Hilgard](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/joe-hilgard/32/1571_2.png) [@Joe-Hilgard](https://discourse.psychopy.org/u/Joe-Hilgard)\
**Post date:** [December 1, 2017, 9:23pm UTC](https://discourse.psychopy.org/t/bugfixes-for-unable-to-share-contexts-and-portaudio-not-initialized/3537/1 "2017-12-01T21:23:51Z")

</div>

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](https://groups.google.com/forum/#!topic/psychopy-users/-qn7HhLY9B4).

```auto
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](https://discourse.psychopy.org/t/a-word-about-pyo-audio-backend/2253/4).

For more details, see

> **[Adventures programming a Word Pronunciation Task in PsychoPy](http://crystalprisonzone.blogspot.com/2017/12/adventures-programming-word.html)**
>
> I'm a new assistant professor trying to set up my research laboratory. I thought I'd try making the jump to PsychoPy as a way to make my mat...

---

<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:** [December 8, 2017, 11:10am UTC](https://discourse.psychopy.org/t/bugfixes-for-unable-to-share-contexts-and-portaudio-not-initialized/3537/2 "2017-12-08T11:10:23Z")

</div>

Could you help me test a possible fix for the pyglet window issue? Could you find the [window.py](http://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:

```python
        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.

---

<div class="post-metadata">

**Author:** ![bjoern](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/bjoern/32/141_2.png) [@bjoern](https://discourse.psychopy.org/u/bjoern)\
**Post date:** [April 11, 2018, 2:10pm UTC](https://discourse.psychopy.org/t/bugfixes-for-unable-to-share-contexts-and-portaudio-not-initialized/3537/3 "2018-04-11T14:10:13Z")

</div>

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](https://github.com/psychopy/psychopy/commit/a1c29d3a) 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.

---

<div class="post-metadata">

**Author:** ![Yeray\_Mera](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/yeray_mera/32/4638_2.png) [@Yeray\_Mera](https://discourse.psychopy.org/u/Yeray_Mera)\
**Post date:** [May 9, 2019, 8:41am UTC](https://discourse.psychopy.org/t/bugfixes-for-unable-to-share-contexts-and-portaudio-not-initialized/3537/4 "2019-05-09T08:41:28Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![mirh](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/mirh/32/4625_2.png) [@mirh](https://discourse.psychopy.org/u/mirh)\
**Post date:** [May 24, 2019, 11:42pm UTC](https://discourse.psychopy.org/t/bugfixes-for-unable-to-share-contexts-and-portaudio-not-initialized/3537/5 "2019-05-24T23:42:18Z")

</div>

> [@bjoern](#):
>
> 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

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

```auto
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

---

<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:** [May 25, 2019, 9:02am UTC](https://discourse.psychopy.org/t/bugfixes-for-unable-to-share-contexts-and-portaudio-not-initialized/3537/6 "2019-05-25T09:02:09Z")

</div>

> [@mirh](#):
>
> 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. 🙂

Thanks

---

<div class="post-metadata">

**Author:** ![mirh](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/mirh/32/4625_2.png) [@mirh](https://discourse.psychopy.org/u/mirh)\
**Post date:** [May 25, 2019, 10:42pm UTC](https://discourse.psychopy.org/t/bugfixes-for-unable-to-share-contexts-and-portaudio-not-initialized/3537/7 "2019-05-25T22:42:45Z")

</div>

AFAIU (also noted in the [sourcecode](https://github.com/psychopy/psychopy/blob/3.1.2/psychopy/visual/backends/pygletbackend.py#L29)) the problem is that options have to be set [before](https://groups.google.com/d/msg/pyglet-users/qUj7JeQhLgQ/QOB6j6lV0dMJ) 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](https://bitbucket.org/pyglet/pyglet/issues/23/windows-some-intel-drivers-do-not-support) this (G33 graphics on XP) is a _bugged_ driver, or just an _overzealous_ one

---

<div class="post-metadata">

**Author:** ![bsobel](https://avatars.discourse-cdn.com/v4/letter/b/ecd19e/32.png) [@bsobel](https://discourse.psychopy.org/u/bsobel)\
**Post date:** [February 12, 2020, 4:40pm UTC](https://discourse.psychopy.org/t/bugfixes-for-unable-to-share-contexts-and-portaudio-not-initialized/3537/8 "2020-02-12T16:40:35Z")

</div>

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.”

---

<div class="post-metadata">

**Author:** ![bjoern](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/bjoern/32/141_2.png) [@bjoern](https://discourse.psychopy.org/u/bjoern)\
**Post date:** [February 13, 2020, 2:43pm UTC](https://discourse.psychopy.org/t/bugfixes-for-unable-to-share-contexts-and-portaudio-not-initialized/3537/9 "2020-02-13T14:43:46Z")

</div>

> [@bsobel](#):
>
> 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.

> [@mirh](#):
>
> Not sure why you actually didn’t follow the instructions in the OP. […]

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.

---

<div class="post-metadata">

**Author:** ![mirh](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/mirh/32/4625_2.png) [@mirh](https://discourse.psychopy.org/u/mirh)\
**Post date:** [February 13, 2020, 5:15pm UTC](https://discourse.psychopy.org/t/bugfixes-for-unable-to-share-contexts-and-portaudio-not-initialized/3537/10 "2020-02-13T17:15:48Z")

</div>

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).
