AttributeError:' NoneType'对象没有属性' flip'在使用健身房时

时间:2018-02-05 18:07:08

标签: python openai-gym

我在开放的AI健身房文档中尝试了代码。它工作但我关闭窗口后发生错误。我使用的代码如下:

import gym
env = gym.make('CartPole-v0')
for i_episode in range(20):
    observation = env.reset()
    for t in range(100):
        env.render()
        print(observation)
        action = env.action_space.sample()
        observation, reward, done, info = env.step(action)
        if done:
            print("Episode finished after {} timesteps".format(t+1))
            break

这是错误:

  File "<ipython-input-3-b38d53bb166b>", line 1, in <module>
    runfile('D:/Python/openAIGym.py', wdir='D:/Python')

  File "D:\Anaconda\envs\tensorflow\lib\site-packages\spyder\utils\site\sitecustomize.py", line 705, in runfile
    execfile(filename, namespace)

  File "D:\Anaconda\envs\tensorflow\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "D:/Python/openAIGym.py", line 6, in <module>
    env.render()

  File "D:\Anaconda\envs\tensorflow\lib\site-packages\gym\core.py", line 150, in render
    return self._render(mode=mode, close=close)

  File "D:\Anaconda\envs\tensorflow\lib\site-packages\gym\core.py", line 286, in _render
    return self.env.render(mode, close)

  File "D:\Anaconda\envs\tensorflow\lib\site-packages\gym\core.py", line 150, in render
    return self._render(mode=mode, close=close)

  File "D:\Anaconda\envs\tensorflow\lib\site-packages\gym\envs\classic_control\cartpole.py", line 146, in _render
    return self.viewer.render(return_rgb_array = mode=='rgb_array')

  File "D:\Anaconda\envs\tensorflow\lib\site-packages\gym\envs\classic_control\rendering.py", line 104, in render
    self.window.flip()

  File "D:\Anaconda\envs\tensorflow\lib\site-packages\pyglet\window\win32\__init__.py", line 311, in flip
    self.context.flip()

AttributeError: 'NoneType' object has no attribute 'flip'

BTW,这是在python 3.6中。

1 个答案:

答案 0 :(得分:0)

我最近也遇到过这个问题。 你应该添加&#34; env.render(close = True)&#34;到最后你的代码,问题就解决了。