已解决VSCode运行强化学习代码抛出异常AttributeError: ‘NoneType’ object has no attribute ‘Window’
pyglet.canvas.xlib.NoSuchDisplayException: Cannot connect to “None” 的正确解决方法,亲测有效!!
报错问题
一个小伙伴遇到问题跑来私信我,在VSCode运行强化学习代码,但是发生了报错(当时他心里瞬间凉了一大截,跑来找我求助,然后顺利帮助他解决了,顺便记录一下希望可以帮助到更多遇到这个bug不会解决的小伙伴),报错代码如下所示:
import pyglet
window = pyglet.window.Window(500, 500)
报错信息内容如下所示:
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"
报错翻译
报错信息内容翻译如下所示:
属性错误:“NoneType”对象没有属性“Window”
pyglet.canvas.xlib.NoSuch显示异常:无法连接到“None”
报错原因
报错原因:
在其他少数情况下,它不是以一种方式运行而不是以另一种方式工作的。大多数解决方案都涉及更改图形卡设置或运行X11服务器。这在这里似乎不相关,因为它似乎只与VSCode相关。
小伙伴们按照下面的方法配置vscode即可即可!!!
解决方法
这是vscode内部终端的问题,将控制台设置为launch.json中的另一个选项解决了这个问题:
{
// 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"
}
]
}
因此,事实上,在python脚本中添加以下内容可以解决问题(至少结合切换到Wayland):
import os
os.environ['DISPLAY'] = ':1'
以上是此问题报错原因的解决方法,欢迎评论区留言讨论是否能解决,如果有用欢迎点赞收藏文章谢谢支持,博主才有动力持续记录遇到的问题!!!
到此这篇关于解决AttributeError: ‘NoneType‘ object has no attribute ‘Window‘的问题(亲测有效)的文章就介绍到这了,更多相关NoneType object has no attribute 内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!