| Reference | Downloads | Github

"Unable to share contexts" error when creating window

I am having trouble with the “Unable to share contexts” error. I have looked at many resources but am still unable to sort this out. Here is the error message I am receiving:

Traceback (most recent call last):
  File "C:\Program Files\PsychoPy3\lib\site-packages\psychopy\visual\backends\", line 146, in __init__
  File "C:\Program Files\PsychoPy3\lib\site-packages\pyglet\window\win32\", line 134, in __init__
    super(Win32Window, self).__init__(*args, **kwargs)
  File "C:\Program Files\PsychoPy3\lib\site-packages\pyglet\window\", line 571, in __init__
  File "C:\Program Files\PsychoPy3\lib\site-packages\pyglet\window\win32\", line 266, in _create
  File "C:\Program Files\PsychoPy3\lib\site-packages\pyglet\gl\", line 267, in attach
    super(Win32ARBContext, self).attach(canvas)
  File "C:\Program Files\PsychoPy3\lib\site-packages\pyglet\gl\", line 212, in attach
    raise gl.ContextException('Unable to share contexts') Unable to share contexts

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "\\\LabDocs\PythonCode_behavioralexperiments\Gavin\New Tortoises\Tortoise_Experiment_1\", line 77, in <module>
    color=[-1,-1,-1], fullscr=True)
  File "C:\Program Files\PsychoPy3\lib\site-packages\psychopy\visual\", line 411, in __init__
    self.backend = backends.getBackend(win=self, *args, **kwargs)
  File "C:\Program Files\PsychoPy3\lib\site-packages\psychopy\visual\backends\", line 32, in getBackend
    return Backend(win, *args, **kwargs)
  File "C:\Program Files\PsychoPy3\lib\site-packages\psychopy\visual\backends\", line 153, in __init__
AttributeError: 'PygletBackend' object has no attribute '_isFullScr'
Exception ignored in: <bound method Window.__del__ of <psychopy.visual.window.Window object at 0x01CB7E30>>
Traceback (most recent call last):
  File "C:\Program Files\PsychoPy3\lib\site-packages\psychopy\visual\", line 545, in __del__
  File "C:\Program Files\PsychoPy3\lib\site-packages\psychopy\visual\", line 1782, in close
    self.backend.close()  # moved here, dereferencing the window prevents
AttributeError: 'NoneType' object has no attribute 'close'

I tried following this fix (Bugfixes for "Unable to share contexts" and "Portaudio not initialized"). I modified the beginning of my code to:

import pyglet
from psychopy import visual, core, data, event, logging, gui, sound

but, I get this error:

Traceback (most recent call last):
  File "\\\LabDocs\PythonCode_behavioralexperiments\Gavin\New Tortoises\Tortoise_Experiment_1\", line 3, in <module>
    from psychopy import visual, core, data, event, logging, gui, sound
  File "C:\Program Files\PsychoPy3\lib\site-packages\psychopy\visual\", line 31, in <module>
    from .window import Window, getMsPerFrame, openWindows
  File "C:\Program Files\PsychoPy3\lib\site-packages\psychopy\visual\", line 102, in <module>
    from . import shaders as _shaders
  File "C:\Program Files\PsychoPy3\lib\site-packages\psychopy\visual\", line 14, in <module>
    import as gltools
  File "C:\Program Files\PsychoPy3\lib\site-packages\psychopy\tools\", line 93, in <module>
    GL.glGenQueries(1, ctypes.byref(QUERY_COUNTER))
  File "C:\Program Files\PsychoPy3\lib\site-packages\pyglet\gl\", line 95, in __call__
    'Call to function "%s" before GL context created' %
Exception: Call to function "glGenQueries" before GL context created

Changing the block of code to occur after importing psychopy removes that error, but the “Unable to share contexts” error still persists.

It works fine on other computers, but not the one that our lab has, which is running:

Python 3.6.7 (v3.6.7:6ec5cf24b7, Oct 20 2018, 12:45:02) [MSC v.1900 32 bit (Intel)] on win32

Thanks in advance!

I think the second error was resolved in the newest version of PsychoPy to fix another issue, but I wasn’t aware that it also affected this work around. If you can edit the file and change …

# create a query counter to get absolute GPU time


GL.glGenQueries(1, ctypes.byref(QUERY_COUNTER))



It should continue loading correctly with the workaround.

The issue here is the work around prevents a GL context from being created until after the gltools module is loaded which causes the error. It’s unlikely that you are using the GPU performance counter so you can safely prevent it from creating the object. The current development version of PsychoPy fixes this issue, so you wont need to edit these lines out in the future.

That solved it. Thank you for your help!