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?

1

6 Answers

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.

4

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

git clone 
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.

1

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

u can try update glfw to glfw 3.3+

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

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Sophia Al-Mansoor

Sophia Al-Mansoor

Global Business & E-Commerce Reporter

Sophia analyzes international trade, startup ecosystems, retail transformation, and supply chain logistics for modern digital publications.

Share this article
Twitter Facebook Pinterest