Display issue when calling render() method in Gym environment

I’m having trouble getting the visualization to work in my reinforcement learning setup. Here’s what I’m trying to run:

import gymnasium as gym

my_env = gym.make("MountainCar-v0")
my_env.reset()
my_env.display()

The first three lines execute without any issues, but when I try to run the display method, I get this error:

Traceback (most recent call last):
  File "<ipython-input-5-b8d3f2e1ac42>", line 1, in <module>
    my_env.display()
  File "/usr/local/gym/gym/core.py", line 148, in display
    return self._display(mode=mode, close=close)
  File "/usr/local/gym/gym/core.py", line 284, in _display
    return self.env.display(mode, close)
  File "/usr/local/gym/gym/core.py", line 148, in display
    return self._display(mode=mode, close=close)
  File "/usr/local/gym/gym/envs/classic_control/mountain_car.py", line 114, in _display
    self.viewer = rendering.Viewer(window_width, window_height)
  File "/usr/local/gym/gym/envs/classic_control/rendering.py", line 49, in __init__
    self.window = pyglet.window.Window(width=width, height=height, display=display)
  File "/usr/local/anaconda3/lib/python3.6/site-packages/pyglet/window/__init__.py", line 502, in __init__
    screen = display.get_default_screen()
  File "/usr/local/anaconda3/lib/python3.6/site-packages/pyglet/canvas/base.py", line 71, in get_default_screen
    return self.get_screens()[0]
  File "/usr/local/anaconda3/lib/python3.6/site-packages/pyglet/canvas/base.py", line 63, in get_screens
    raise NotImplementedError('abstract')
NotImplementedError: abstract

I expected to see the mountain car environment open in a new window for visualization. I’m running this in PyCharm on Ubuntu 18.04 with Python 3.6.3 and IPython 6.1.0. I’ve also tried running it in VS Code but got the same result. I’m pretty new to machine learning and just started working with these environments yesterday.

This issue is rooted in the display backend of Ubuntu 18.04, as Pyglet may be struggling to access the X server. Instead of using my_env.display(), consider using my_env.render(mode='human'), which should work for visualizing the environment. If that still doesn’t work, you might want to set up a virtual display using xvfb. You can install it with sudo apt-get install xvfb and run your script with xvfb-run -s "-screen 0 1400x900x24" python your_script.py. Alternatively, using mode='rgb_array' allows you to capture pixel data without opening a window, making it suitable for headless environments.

I hit the same issue switching from old Gym to Gymnasium. You’re using an outdated method - display() got replaced with render(). Change your last line to my_env.render() instead of my_env.display(). That Pyglet error happens a lot on Ubuntu when the GUI isn’t set up right. If render() still breaks, try pip install gymnasium[classic_control] to get all the rendering stuff. Also check if you’re SSH’d in without X11 forwarding - that’ll block any GUI windows from showing up.