9

I am trying to get the code below to work.

import gym

env = gym.make("CartPole-v0")

env.reset()

env.render()

I have no problems running the first 3 lines but when I run the 4th I get the error:

Traceback (most recent call last):

  File "<ipython-input-3-a692a1a1ffe7>", line 1, in <module>
    env.render()

  File "/home/mikedoho/gym/gym/core.py", line 150, in render
    return self._render(mode=mode, close=close)

  File "/home/mikedoho/gym/gym/core.py", line 286, in _render
    return self.env.render(mode, close)

  File "/home/mikedoho/gym/gym/core.py", line 150, in render
    return self._render(mode=mode, close=close)

  File "/home/mikedoho/gym/gym/envs/classic_control/cartpole.py", line 116, in _render
    self.viewer = rendering.Viewer(screen_width, screen_height)

  File "/home/mikedoho/gym/gym/envs/classic_control/rendering.py", line 51, in __init__
    self.window = pyglet.window.Window(width=width, height=height, display=display)

  File "/home/mikedoho/anaconda3/lib/python3.6/site-packages/pyglet/window/__init__.py", line 504, in __init__
    screen = display.get_default_screen()

  File "/home/mikedoho/anaconda3/lib/python3.6/site-packages/pyglet/canvas/base.py", line 73, in get_default_screen
    return self.get_screens()[0]

  File "/home/mikedoho/anaconda3/lib/python3.6/site-packages/pyglet/canvas/base.py", line 65, in get_screens
    raise NotImplementedError('abstract')

NotImplementedError: abstract

I was hoping to get a cart on a pole to display in a new window.

This error is from code in Spyder. I am using ubuntu 16.04; python 3.6.3; IPython 6.1.0. I have not any luck using jupyter notebook.

Again I apologize. I am very new at this an just downloaded ubuntu today.

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
MikeDoho
  • 552
  • 2
  • 5
  • 18

1 Answers1

15

I just resolved this (bug?), like 10 minutes ago :D

You have to downgrade pyglet 3 to pyglet 1.2.4. just type: pip install pyglet==1.2.4 in your anaconda prompt. Afterwards you might have to restart your spyder ide.

I would add env.close() to your code, otherwise you will have troubles closing the window.

if you are new to python/ML/DL have a look at "sentdex" on youtube. imo very good content to start with. (he uses TFlearn in his gym tutorial) But since tensorflow 1.4 has Keras included, i would suggest to use keras high lvl API for your neural network.

good luck and stay deep

if someone has a good gym documentation, feel free to share it.

Rajan Kali
  • 12,627
  • 3
  • 25
  • 37
mGwork
  • 166
  • 1
  • 3
  • Thank you so much! I can't wait to try this soon. Also thank you for the recommendation. I will make sure to look at those YouTube videos. Also i will make sure to update my response whether or not this works but I'm hopeful. Thanks again! – MikeDoho Dec 03 '17 at 19:09
  • So it worked! Thanks again! No error after running. I just need to find out where the screen is suppose to pop up. I would add my code but I cant get it to format well but I ran the same code as I did original (after making your changes) and I got [2017-12-03 14:34:17,063] Making new env: CartPole-v0 without any errors. – MikeDoho Dec 03 '17 at 19:36
  • env.render(close = True) closes the window. Most likely it pops up and closes again (in your code). Just comment it out and you will see the window. – mGwork Dec 03 '17 at 21:35
  • Awesome I'll give this a try too! By the way how do you find all these fixes out? Is there a way that I can systematically go through the code to determine things I should look up? Again I'm very new at this. I can't thank you enough for your advice so far. My strategy currently is copy and paste the error into Google and Explorer links. – MikeDoho Dec 03 '17 at 21:41
  • I tried to comment out the code as you suggested import gym env = gym.make("CartPole-v0") env.reset() leaving me with what I pasted but it didnt work. That is ok though. I will toy with it somemore. thanks – MikeDoho Dec 04 '17 at 03:08
  • Sorry for the onslaught of comments. I can see the window now. Followed your instructions again and for some reason it worked this time. I must have messed up the first time! – MikeDoho Dec 05 '17 at 14:39
  • For 2020 readers ( Gym version 0.17.2 ), downgrade just to 1.4.0 and not 1.2.4 otherwise you will have other errors. – ADEL NAMANI Sep 14 '20 at 00:49