9

I've just installed openAI gym on Google Colab, but when I try to run 'CartPole-v0' environment as explained here.

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

I get this:

WARN: gym.spaces.Box autodetected dtype as <class 'numpy.float32'>. Please provide explicit dtype.
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-19-a81cbed23ce4> in <module>()
      4     observation = env.reset()
      5     for t in range(100):
----> 6         env.render()
      7         print(observation)
      8         action = env.action_space.sample()

/content/gym/gym/core.py in render(self, mode)
    282 
    283     def render(self, mode='human'):
--> 284         return self.env.render(mode)
    285 
    286     def close(self):

/content/gym/gym/envs/classic_control/cartpole.py in render(self, mode)
    104 
    105         if self.viewer is None:
--> 106             from gym.envs.classic_control import rendering
    107             self.viewer = rendering.Viewer(screen_width, screen_height)
    108             l,r,t,b = -cartwidth/2, cartwidth/2, cartheight/2, -cartheight/2

/content/gym/gym/envs/classic_control/rendering.py in <module>()
     21 
     22 try:
---> 23     from pyglet.gl import *
     24 except ImportError as e:
     25     reraise(prefix="Error occured while running `from pyglet.gl import *`",suffix="HINT: make sure you have OpenGL install. On Ubuntu, you can run 'apt-get install python-opengl'. If you're running on a server, you may need a virtual frame buffer; something like this should work: 'xvfb-run -s \"-screen 0 1400x900x24\" python <your_script.py>'")

/usr/local/lib/python3.6/dist-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

The problem is the same in this question about NameError in openAI gym

Nothing is being rendered. I don't know how I could use this in google colab: 'xvfb-run -s \"-screen 0 1400x900x24\" python <your_script.py>'"

5 Answers5

12

One way to render gym environment in google colab is to use pyvirtualdisplay and store rgb frame array while running environment. Environment frames can be animated using animation feature of matplotlib and HTML function used for Ipython display module. You can find the implementation here. Make sure you install required libraries which you can find in the first cell of the colab. In case the first link for google colab doesn't work you can see this one.

  • Super helpful. Until we get WebGL rendering, this is definitely the way to go for anyone stumbling in here. – NegatioN Aug 10 '18 at 21:26
  • @Yograj It's working fine. But can you add few other lines of code that install Box2D too. CartPole is running fine on it but when I use CarRacing it throws error. I tried installing Box2D from various sources, but failed all the time. – Krishna Sep 12 '18 at 09:32
  • @सत्यमेवजयते Did you happen to figure out a solution for the box2 environments? – johannesack Jun 01 '20 at 14:35
  • I don't exactly remember, how I did it. Good luck finding it. – Krishna Jun 03 '20 at 09:54
  • Sorry, the file you have requested does not exist. – elexhobby Mar 08 '22 at 01:30
  • For anyone that is reading this in 2023, I have a working implementation on my github page using Gymnasium (the current maintained fork of Open AI gym) https://github.com/hom-bahrani/Gymnasium-Colaboratory-Starter – Hom Bahrani Mar 27 '23 at 10:17
7

The Gym will normally render the display using GL, on your screen.

But Colab is run on the web as a notebook, it can’t display directly to your screen. It can only show the result through HTML.

If someone modifies the Gym to maniplulate WebGL, maybe someday. But not now.

korakot
  • 37,818
  • 16
  • 123
  • 144
1

i saw an suitablbe answer here.

pip install pyglet==1.5.11
alkaid
  • 21
  • 1
0

Javier, Could you find any solution to this issue? I am trying to use the OenAIs new environment "gym retro" and getting same kind of error when calling make. But as you said I think using xvfb should resolve the issue and let the program run, but of course we wouldn't be able to see the environment graphically. But the issue is that does not allow xvfb to run in the background! xvfb :99 & raises OSError: Background processes not supported.

user650585
  • 313
  • 2
  • 3
  • 10
  • Hi! Not at the moment. I think Korakot is right, Gym should be modified to manipulate webGL and then we would be able to see our algorithm in action. I' ve been thinking of training an algorithm without calling env.render() and try to use the monitor to generate a video file of the experience. What do you think about that? – Javier A. Zambrano Macias Apr 08 '18 at 08:30
  • Hi. Well I just tried it without calling render and it seems to be working. But since I am very new to gym environment, when it comes to recording and playing it back , are you talking about the playback section in this page: https://github.com/openai/retro? – user650585 Apr 08 '18 at 17:30
  • Hi, I am talking about something like this https://discuss.openai.com/t/way-to-train-without-opening-a-window/1188 – Javier A. Zambrano Macias Apr 09 '18 at 17:14
  • I am not sure if what is explained in the playback section is the same. Have you tried it to? – Javier A. Zambrano Macias Apr 09 '18 at 17:24
  • This is not an answer , it is a comment , or rather a question – Dhia Hassen Dec 21 '19 at 21:21
0

Just copy paste this

%%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.*

#import
import pyvirtualdisplay
_display = pyvirtualdisplay.Display(visible=0,  # remember to use visible=0 and not False
                                    size=(1400, 900))
_ = _display.start()

#check
!echo $DISPLAY
brewiee
  • 1
  • 2