[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?
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.
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.
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With