运行代码:

import gym

def cartpole():
environment = gym.make('CartPole-v1')
environment.reset()
for _ in range(1000):
environment.render()
action = environment.action_space.sample()
observation, reward, done, info = environment.step(action)
print("Step {}:".format(_))
print("action: {}".format(action))
print("observation: {}".format(observation))
print("reward: {}".format(reward))
print("done: {}".format(done))
print("info: {}".format(info))

if __name__ == '__main__':

cartpole()


报错,提示需要安装pyglet:

运行openai的gym代码报错提示import pyglet,安装后依然报错:ImportError: s_重新安装

安装pyglet:

pip install pyglet

运行openai的gym代码报错提示import pyglet,安装后依然报错:ImportError: s_Python_02

再次运行依然报错:

运行openai的gym代码报错提示import pyglet,安装后依然报错:ImportError: s_Python_03

最后发现其原因在pyglet的版本问题:

卸载pyglet并重新安装即可解决:

pip uninstall pyglet

pip install pyglet==1.2.4  

成功解决错误。