时间:2023-03-17来源:系统城装机大师作者:佚名
已解决VSCode运行强化学习代码抛出异常AttributeError: ‘NoneType’ object has no attribute ‘Window’
pyglet.canvas.xlib.NoSuchDisplayException: Cannot connect to “None” 的正确解决方法,亲测有效!!
一个小伙伴遇到问题跑来私信我,在VSCode运行强化学习代码,但是发生了报错(当时他心里瞬间凉了一大截,跑来找我求助,然后顺利帮助他解决了,顺便记录一下希望可以帮助到更多遇到这个bug不会解决的小伙伴),报错代码如下所示:
1 2 |
import pyglet window = pyglet.window.Window( 500 , 500 ) |
报错信息内容如下所示:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
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中的另一个选项解决了这个问题:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
{ / / 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):
1 2 |
import os os.environ[ 'DISPLAY' ] = ':1' |
以上是此问题报错原因的解决方法,欢迎评论区留言讨论是否能解决,如果有用欢迎点赞收藏文章谢谢支持,博主才有动力持续记录遇到的问题!!!
2023-03-17
python flask项目打包成docker镜像发布的过程2023-03-17
python调试模块ipdb详解2023-03-17
python使用openai生成图像的超详细教程python cron定时任务触发接口自动化巡检 apscheduler报错:Run time of job …… next run at: ……)” was missed by misfire_grace_time参数 找到任务超时的根本原因...
2023-03-15