If I run this code in the VSCode terminal
import pyglet
window = pyglet.window.Window(500, 500)
I get
Traceback (most recent call last):
File "/home/justin/.local/lib/python3.8/site-packages/pyglet/__init__.py", line 334, in __getattr__
return getattr(self._module, name)
AttributeError: 'NoneType' object has no attribute 'Window'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/justin/Dropbox/jr/py/pyglet_games/pyglet_demo/displaytest.py", line 5, in <module>
window = pyglet.window.Window(500, 500)
File "/home/justin/.local/lib/python3.8/site-packages/pyglet/__init__.py", line 340, in __getattr__
__import__(import_name)
File "/home/justin/.local/lib/python3.8/site-packages/pyglet/window/__init__.py", line 1891, in <module>
gl._create_shadow_window()
File "/home/justin/.local/lib/python3.8/site-packages/pyglet/gl/__init__.py", line 220, in _create_shadow_window
_shadow_window = Window(width=1, height=1, visible=False)
File "/home/justin/.local/lib/python3.8/site-packages/pyglet/window/xlib/__init__.py", line 171, in __init__
super(XlibWindow, self).__init__(*args, **kwargs)
File "/home/justin/.local/lib/python3.8/site-packages/pyglet/window/__init__.py", line 573, in __init__
display = pyglet.canvas.get_display()
File "/home/justin/.local/lib/python3.8/site-packages/pyglet/canvas/__init__.py", line 94, in get_display
return Display()
File "/home/justin/.local/lib/python3.8/site-packages/pyglet/canvas/xlib.py", line 123, in __init__
raise NoSuchDisplayException('Cannot connect to "%s"' % name)
pyglet.canvas.xlib.NoSuchDisplayException: Cannot connect to "None"
But if I run it in on the command line a window appears as expected. It also works as expected if I run it in Thonny, which I use to teach. I have verified that sys.version and sys.path are identical when run in the terminal or in VSCode.
I've searched for solutions to this problem but in the few other cases that exist it's not the case that it works when run in one way and not in another. Most of the solutions involve changing graphics card settings or running an X11 server. This doesn't seem to be relevant here since it seems to be related only to VSCode.
Thanks.