27

[Note that I am using xvfb-run -s "-screen 0 1400x900x24" jupyter notebook]

I try to run a basic set of commands in OpenAI Gym

import gym
env = gym.make("CartPole-v0")
obs = env.reset()
env.render()

but I get the following error:

...

~/Downloads/yes/lib/python3.7/site-packages/pyglet/gl/__init__.py in <module>()
    225     else:
    226         from .carbon import CarbonConfig as Config
--> 227 del base
    228 
    229 # XXX remove

NameError: name 'base' is not defined

What can I do to fix this?

midawn98
  • 401
  • 1
  • 4
  • 8

6 Answers6

13

Solving your issue required getting the right combination of system dependencies and python dependencies. Paste this code into a cell in Colab and run it to install all of the dependencies (taking note of the specific version numbers used).

%%bash

# install required system dependencies
apt-get install -y xvfb x11-utils

# install required python dependencies (might need to install additional gym extras depending)
pip install gym[box2d]==0.17.* pyvirtualdisplay==0.2.* PyOpenGL==3.1.* PyOpenGL-accelerate==3.1.*

The final step is to run the following block of code to properly initialize the virtual display. The code in the below creates a virtual display in the background that your Gym Envs can connect to for rendering. You can adjust the size of the virtual buffer as you like but you must set visible=False when working with Xvfb.

This code only needs to be run once per session to start the display.

import pyvirtualdisplay


_display = pyvirtualdisplay.Display(visible=False,  # use False with Xvfb
                                    size=(1400, 900))
_ = _display.start()

For additional details check out the following blog post.

davidrpugh
  • 4,363
  • 5
  • 32
  • 46
  • 3
    I got `Successfully installed EasyProcess-0.3 PyOpenGL-accelerate-3.1.5 box2d-py-2.3.8 pyvirtualdisplay-0.2.5` after running your commands. However still getting `NameError: name 'base' is not defined` – Axel Bregnsbo Oct 31 '20 at 10:58
  • you miss the last part from the blog post. These commands also needs to be executed in Colab: `import pyvirtualdisplay _display = pyvirtualdisplay.Display(visible=False, size=(1400, 900)) _ = _display.start()` – Axel Bregnsbo Oct 31 '20 at 12:15
  • I had to run `apt-get install swig` before running the pip install. But otherwise this solved it for me – L.Lauenburg May 17 '21 at 15:33
  • I continue to get the error in spite of pasting both these cells. See https://imgur.com/UvnOqFI for the screenshot and error logs. – elexhobby Mar 08 '22 at 01:14
  • Btw you will have to restart notebook. to make it work too – Vova Bilyachat Apr 14 '23 at 07:55
6

It's work for me. (And I just met the same problem)

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

You can also have a try,

conda install -c conda-forge pyglet
# pyglet==1.2.4?

Before that, I installed gym with pip, maybe this is the problem.

Francis QING
  • 63
  • 1
  • 4
2

change the code as follow

import gym
print(gym.__version__)# for me: 0.15.4
env = gym.make("CartPole-v0")
obs = env.reset()
for i in range(1000):# it's changable
    env.step(env.action_space.sample())
    env.render()# won't work in Google Colab
env.close()
Ali
  • 112
  • 1
  • 3
  • 2
    maybe it's from your system, check your gym version. note that render function won't work on colab and you need IPython notebook tricks to display it - or save a video from rendering – Ali Mar 28 '20 at 19:19
1

This might work for you:

Uninstall the packages with these commands:

pip uninstall pyglet
pip uninstall gym

Then install the packages using these commands:

conda install -c conda-forge pyglet
conda install -c conda-forge gym
JW Geertsma
  • 857
  • 3
  • 13
  • 19
Yilu
  • 11
  • 2
0

u can try update glfw to glfw 3.3+

Kuner
  • 11
  • 1
-1

You could run the algorithm from the command line:

python -m spinup.run ppo --exp_name CartPole --env CartPole-v0

Here, ppo is the proximal policy optimization algorithm, but you can run any of the algorithms you want.