midawn98
midawn98

Reputation: 401

NameError: name 'base' is not defined OpenAI Gym

[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?

Upvotes: 27

Views: 37348

Answers (6)

Yilu
Yilu

Reputation: 11

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

Upvotes: 1

davidrpugh
davidrpugh

Reputation: 4553

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.

Upvotes: 13

Kuner
Kuner

Reputation: 11

u can try update glfw to glfw 3.3+

Upvotes: 0

Ali
Ali

Reputation: 141

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()

Upvotes: 2

Francis QING
Francis QING

Reputation: 63

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.

Upvotes: 6

user10672606
user10672606

Reputation:

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.

Upvotes: -1

Related Questions