I installed gym in a virtualenv, and ran a script that was a copy of the first step of the tutorial. After the first iteration, it quite after it raised an exception: ImportError: sys.meta_path is None, Python is likely shutting down, after the warning
WARN: You are calling 'step()' even though this environment has already returned done = True. You should always call 'reset()' once you receive 'done = True' -- any further steps are undefined behavior.
Python version is 3.7.0, gym version os 0.10.5. The exact file I ran, and all the versions, can be found in gist https://gist.github.com/jagill/5aa64abac2d9bc81ec1d16ab4fa7ee1c
This was after a reboot, so things should be relatively clean.
Btw, calling if done: env.reset() (as the warning suggests) mostly solves the problem. The system exits with the same ImportError after the last iteration.
The same issue on Windows 10, Python 3.7.0
Your fix is correct, you should be able to fix the pyglet error by closing the environment:
import gym
env = gym.make('CartPole-v0')
env.reset()
for _ in range(1000):
env.render()
obs, rew, done, info = env.step(env.action_space.sample()) # take a random action
if done:
env.reset()
env.close()
You do not have to reset env everytime, it is just it makes it so fast and does not close it at the end of a loop, just simply add time.sleep()
import gym
import time
env = gym.make('CartPole-v0')
env.reset()
for i in range(500):
env.render()
env.step(env.action_space.sample()) # take a random action
time.sleep(0.02)
if i%10==0: print(i)
env.close()
Most helpful comment
Your fix is correct, you should be able to fix the pyglet error by closing the environment: