pyglet.canvas.xlib.NoSuchDisplayException: Cannot connect to "None" only happens in VSCode terminal

Viewed 5863

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.

2 Answers

I was having the same problem, not just with pyglet but with anything that tried to open a window.

It looks to be a problem with vs code's internal terminal. Setting console to an alternative in launch.json fixed it for me.

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "externalTerminal"
        }
    ]
}

So indeed, adding the following to my python script sorts the problem (at least in combination with switching to Wayland):

import os
os.environ['DISPLAY'] = ':1'

Does anyone know how to make this a permanent setting so that it's not required in every module I write that needs to output to the display?

In vscode you can use "Run Current File in Interactive Window" by right clicking on python code.

Right most window is the case where I get error when I use "Run Python File in Terminal"

vscode: python code execution

Related