Search code examples
pythonopenai-gym

Open AI gym environments don't render, don't show at all


So I wanted to try some reinforcement learning, I haven't coded anything for a while. On Jupiter Notebooks when I run this code

import gym
env = gym.make("MountainCar-v0")
env.reset()

done = False
while not done:
    action = 2  # always go right!
    env.step(action)
    env.render()

it just tries to render it but can't, the hourglass on top of the window is showing but it never renders anything, I can't do anything from there.

Same with this code

import gym
env_name = "MountainCar-v0"
env = gym.make(env_name)

env.reset()

for _ in range(200)
    action = env.action_space.sample()
    env.step(action)
    env.render()

Both of these don't work neither on Jupiter notebooks nor Pycharm nor terminal. I'm on Windows. Couldn't find anything similar to this online. Yes I'm new to this

Edit- I did this

# Install latest stable version from PyPI
!pip install -U pysdl2

# Install latest development verion from GitHub
!pip install -U git+https://github.com/py-sdl/py-sdl2.git

and it now says error: windlib not available

I tried

!pip install windlib

but still can't fix the error


Solution

  • Use an older version that supports your current version of Python. I solved the problem using gym 0.17.3

    pip install gym==0.17.3
    

    and the code:

    import gym
    
    env = gym.make("MountainCar-v0")
    state = env.reset()
    
    done = False
    while not done:
        action = 2  # always go right!
        env.step(action)
        print(new_state, render)
        env.render(mode="human")
    
    env.close()