强化学习调用gym库时出现的一个报错小问题
问题KeyError: (<weakref at 0x0000012E8CC759A8; to 'Win32Window' at 0x0000012EFC976278>...的解决方案
一、问题描述
程序能正常运行、没什么问题,但是当程序运行结束的时候,却出现以下令人难受的报错:
Exception ignored in: <bound method Viewer.__del__ of <gym.envs.classic_control.rendering.Viewer object at 0x0000012EFC8D99E8>>
Traceback (most recent call last):
File "D:\root\python3.6.3\lib\site-packages\gym\envs\classic_control\rendering.py", line 165, in __del__
self.close()
File "D:\root\python3.6.3\lib\site-packages\gym\envs\classic_control\rendering.py", line 83, in close
self.window.close()
File "D:\root\python3.6.3\lib\site-packages\pyglet\window\win32\__init__.py", line 319, in close
super(Win32Window, self).close()
File "D:\root\python3.6.3\lib\site-packages\pyglet\window\__init__.py", line 838, in close
app.windows.remove(self)
File "D:\root\python3.6.3\lib\_weakrefset.py", line 109, in remove
self.data.remove(ref(item))
KeyError: (<weakref at 0x0000012E8CC759A8; to 'Win32Window' at 0x0000012EFC976278>,)
Exception ignored in: <bound method Viewer.__del__ of <gym.envs.classic_control.rendering.Viewer object at 0x0000012EFC8D99E8>>
Traceback (most recent call last):
File "D:\root\python3.6.3\lib\site-packages\gym\envs\classic_control\rendering.py", line 165, in __del__
self.close()
File "D:\root\python3.6.3\lib\site-packages\gym\envs\classic_control\rendering.py", line 83, in close
self.window.close()
File "D:\root\python3.6.3\lib\site-packages\pyglet\window\win32\__init__.py", line 319, in close
super(Win32Window, self).close()
File "D:\root\python3.6.3\lib\site-packages\pyglet\window\__init__.py", line 838, in close
app.windows.remove(self)
File "D:\root\python3.6.3\lib\_weakrefset.py", line 109, in remove
self.data.remove(ref(item))
KeyError: (<weakref at 0x0000012E8CC759A8; to 'Win32Window' at 0x0000012EFC976278>,)
二、解决方案
出现这样的问题,其实是因为env.close()没有得到正确的使用。而正确使用的方式如下:
1.定义close()函数
2.当程序运行结束时调用env.close()
比如我定义了一个下面这样的环境类:
class GridWorldEnv(gym.Env):
def you_function():
pass
# ...
同时我需要在这个环境类中定义close函数:
class GridWorldEnv(gym.Env):
def you_function():
pass
def close(self):
if self.viewer:
self.viewer.close()
self.viewer = None
# ...
然后我是如何创建环境实例并进行学习的呢?如下:
if __name__ == "__main__":
env = GridWorldEnv()
agent = Agent(env)
# agent learning
# after learning, then you can close the env
agent.env.close() # 调用此行,关闭环境
最后再运行,程序就没有以上的报错了。
三、参考
ImportError: sys.meta_path is None, Python is likely shutting down · Issue #893 · openai/gym · GitHub中回答"nicehiro commented on 24 Aug 2019"
更多推荐
所有评论(0)