"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\pygletbackend.py", line 146, in __init__
    style=style)
  File "C:\Program Files\PsychoPy3\lib\site-packages\pyglet\window\win32\__init__.py", line 134, in __init__
    super(Win32Window, self).__init__(*args, **kwargs)
  File "C:\Program Files\PsychoPy3\lib\site-packages\pyglet\window\__init__.py", line 571, in __init__
    self._create()
  File "C:\Program Files\PsychoPy3\lib\site-packages\pyglet\window\win32\__init__.py", line 266, in _create
    self.context.attach(self.canvas)
  File "C:\Program Files\PsychoPy3\lib\site-packages\pyglet\gl\win32.py", line 267, in attach
    super(Win32ARBContext, self).attach(canvas)
  File "C:\Program Files\PsychoPy3\lib\site-packages\pyglet\gl\win32.py", line 212, in attach
    raise gl.ContextException('Unable to share contexts')
pyglet.gl.ContextException: Unable to share contexts

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "\\130.126.183.19\LabDocs\PythonCode_behavioralexperiments\Gavin\New Tortoises\Tortoise_Experiment_1\untitled.py", line 77, in <module>
    color=[-1,-1,-1], fullscr=True)
  File "C:\Program Files\PsychoPy3\lib\site-packages\psychopy\visual\window.py", line 411, in __init__
    self.backend = backends.getBackend(win=self, *args, **kwargs)
  File "C:\Program Files\PsychoPy3\lib\site-packages\psychopy\visual\backends\__init__.py", line 32, in getBackend
    return Backend(win, *args, **kwargs)
  File "C:\Program Files\PsychoPy3\lib\site-packages\psychopy\visual\backends\pygletbackend.py", line 153, in __init__
    fullscreen=self._isFullScr,
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\window.py", line 545, in __del__
    self.close()
  File "C:\Program Files\PsychoPy3\lib\site-packages\psychopy\visual\window.py", 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
pyglet.options['shadow_window']=False
from psychopy import visual, core, data, event, logging, gui, sound

but, I get this error:

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


# create a query counter to get absolute GPU time

QUERY_COUNTER = GL.GLuint()

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

to

QUERY_COUNTER = None

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!