NameError when trying to render in OpenAI Gym on WSL with Python

I’m facing issues while using OpenAI Gym on Windows Subsystem for Linux with Python version 3.5.2. My attempt to set up and run a basic environment works smoothly until I reach the rendering section. Below is the example code I am working with:

import gym

env = gym.make('MountainCar-v0')
for episode in range(10):
    state = env.reset()
    for step in range(200):
        env.render()
        print(state)
        move = env.action_space.sample()
        state, score, finished, details = env.step(move)
        if finished:
            print("Run completed after {} steps".format(step+1))
            break

The error I encounter is:

Traceback (most recent call last):
  File "<stdin>", line 6, in <module>
  File "/home/user/gym/gym/core.py", line 153, in render
    return self._render(mode=mode, close=close)
  File "/home/user/gym/gym/core.py", line 285, in _render
    return self.env.render(mode, close)
  File "/home/user/gym/gym/core.py", line 153, in render
    return self._render(mode=mode, close=close)
  File "/home/user/gym/gym/envs/classic_control/mountain_car.py", line 114, in _render
    from gym.envs.classic_control import rendering
  File "/home/user/gym/gym/envs/classic_control/rendering.py", line 23, in <module>
    from pyglet.gl import *
  File "/home/user/.local/lib/python3.5/site-packages/pyglet/gl/__init__.py", line 224, in <module>
    del base
NameError: name 'base' is not defined

It looks like there is a problem with the pyglet library causing the rendering process to fail. No visual output is displayed, and I’m unsure how to resolve this issue. Has anyone experienced something similar or found a workaround?

Hit this same pyglet issue on WSL last year. The namespace error pops up because WSL doesn’t have proper OpenGL support out of the box, and pyglet needs it. I fixed it by installing mesa utilities: sudo apt-get install mesa-utils libgl1-mesa-glx. Also try setting export LIBGL_ALWAYS_INDIRECT=1 before you run your script. If you’re still getting that base variable error, downgrade pyglet to 1.2.4 - it plays nicer with WSL’s crappy graphics stack. Rendering’s slow but it works.

I hit this exact problem with OpenAI Gym on WSL about a year ago. It’s a pyglet compatibility issue with older Python versions in WSL - pyglet namespace conflicts cause that ‘base’ variable error. Upgrading to Python 3.7+ fixed it for me. If you can’t upgrade, try pip install pyglet==1.3.2 - it’s more stable with Python 3.5. Alternatively, skip rendering during training by removing the env.render() call. Train headless, then export your model and test it on native Windows where rendering actually works.

Had the same problem with WSL last month. Set your DISPLAY variable first: export DISPLAY=:0, then grab Xming for Windows to handle X11 forwarding. WSL1 doesn’t support GUI stuff out of the box, but pyglet needs it. Works fine once you get that running, though rendering’s a bit sluggish.