本文介绍了openAi-gym NameError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 WSL 上使用 OpenAI 著名的Gym"模块,并在 python 3.5.2 上执行代码.
当我尝试运行环境如此处所述时,使用代码:

I am trying to use the famous 'Gym' module from OpenAI on WSL and executing code on python 3.5.2.
When I try to run an environment as explained here, using the code:

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

发生这种情况:

Traceback (most recent call last):
  File "<stdin>", line 4, in <module>
  File "/home/DrHofstadter/gym/gym/core.py", line 153, in render
    return self._render(mode=mode, close=close)
  File "/home/DrHofstadter/gym/gym/core.py", line 285, in _render
    return self.env.render(mode, close)
  File "/home/DrHofstadter/gym/gym/core.py", line 153, in render
    return self._render(mode=mode, close=close)
  File "/home/DrHofstadter/gym/gym/envs/classic_control/cartpole.py", line 114, in _render
    from gym.envs.classic_control import rendering
  File "/home/DrHofstadter/gym/gym/envs/classic_control/rendering.py", line 23, in <module>
    from pyglet.gl import *
  File "/home/DrHofstadter/.local/lib/python3.5/site-packages/pyglet/gl/__init__.py", line 224, in <module>
    del base
NameError: name 'base' is not defined

问题类似于这个问题什么都没有呈现.(有问题的 gitterforum 链接不再有效.)

The problem is similar to this questionnothing is being rendered. (The gitterforum link given in question doesn't work anymore.)

推荐答案

请向我们展示 pyglet 和gym 版本,我们可以比较它们.您可以删除所有健身房,然后使用 pip install 'gym[all]' 重新安装.此外,如果您在 Colab 或 Jupyter 上工作,您可以添加如下所示的显示(我认为您在笔记本上工作)您可以添加一些支持,例如 xvfbopengl 以支持虚拟显示.
如果你使用Linux基本上安装

Please show us pyglet and gym versions and we can compare them. You can delete all gym and after reinstall with pip install 'gym[all]'. Additionally, if you work on Colab or Jupyter you can add a display like below ( I think you work on a notebook)you can add some supports like xvfb and opengl for the support virtual display.
If you use Linux basically install with

apt-get install -y xvfb python-opengl > /dev/null 2>&1
pip install gym pyvirtualdisplay > /dev/null 2>&1

之后,你必须像下面的格式一样改变你的代码

after that, you must change your code like below format

图书馆

import gym
import numpy as np
import matplotlib.pyplot as plt
from IPython import display as ipythondisplay
from pyvirtualdisplay import Display

开始虚拟显示

display = Display(visible=0, size=(400, 300))
display.start()

现在完成移动

env = gym.make('CartPole-v0')
for i_episode in range(20):
   observation = env.reset()
   for t in range(100):
      plt.imshow(env.render(mode='rgb_array'))# CHANGED
      ipythondisplay.clear_output(wait=True) # ADDED
      ipythondisplay.display(plt.gcf()) # ADDED
      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

这篇关于openAi-gym NameError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-18 01:47