6

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.

retrosnob
  • 113
  • 1
  • 7
  • Did we ever solve this? I am having this exact issue, and neither of the provided solutions work for me. Running Arch Linux on an X11 system with BSPWM. – james Jan 13 '21 at 06:05

4 Answers4

2

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?

karel
  • 5,489
  • 46
  • 45
  • 50
Jonny
  • 51
  • 4
1

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

Vishal
  • 273
  • 1
  • 4
  • 14
1

Try this:

import platform
if platform.system() == "Linux":
    os.environ['PYOPENGL_PLATFORM'] = 'egl'
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 28 '23 at 03:26
0

If you are having this same issue, and working in a google Colab/python environment updating pyglet to this version fixed it for me.

!pip install pyglet==1.5.27
starball
  • 20,030
  • 7
  • 43
  • 238
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 02 '23 at 22:05