29

我正在尝试在 WSL 上使用来自 OpenAI 的著名“Gym”模块并在 python 3.5.2 上执行代码。
当我尝试按照此处的说明运行环境时,使用代码:

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 链接不再起作用。)

4

3 回答 3

1

请试试

git clone https://github.com/openai/gym.git
cd gym
pip install -e .

或者,

pip install pyglet
于 2020-10-01T09:15:17.173 回答
1

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

Libraries

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

Started virtual display

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

Now Finish move

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
于 2021-02-04T19:19:14.073 回答
1

这可能不是这个问题的完美答案,但这是我解决这个问题的经验。

当我从 spyder 运行我的程序时,我遇到了同样的错误。但是当我从终端执行相同的代码时,它没有抛出任何错误。但要为健身房环境正确配置您的语言环境。

于 2020-03-28T04:56:23.703 回答