openAi-gym NameError openAi-gym NameError python python

openAi-gym NameError


This may not be a perfect answer to this question but this is my experience how I resolved this problem.

I was getting the same error when I running my programme from the spyder. but when I execute the same code from the terminal it didn't throw any error. but make your locale is correctly configured for the gym environment.


Please try

git clone https://github.com/openai/gym.gitcd gympip install -e .

Or,

pip install pyglet


Please show us pyglet and gym versions and we can compare them. You can delete all gym and after reinstall with pip install 'gym[all]'. Additionally, if you work on Colab or Jupyter you can add a display like below ( I think you work on a notebook)you can add some supports like xvfb and opengl for the support virtual display.
If you use Linux basically install with

apt-get install -y xvfb python-opengl > /dev/null 2>&1pip install gym pyvirtualdisplay > /dev/null 2>&1

after that, you must change your code like below format

Libraries

import gymimport numpy as npimport matplotlib.pyplot as pltfrom IPython import display as ipythondisplayfrom pyvirtualdisplay import Display

Started virtual display

display = Display(visible=0, size=(400, 300))display.start()

Now Finish move

env = gym.make('CartPole-v0')for i_episode in range(20):   observation = env.reset()   for t in range(100):      plt.imshow(env.render(mode='rgb_array'))# CHANGED      ipythondisplay.clear_output(wait=True) # ADDED      ipythondisplay.display(plt.gcf()) # ADDED      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