Gym: Game window hangs up

Created on 4 Oct 2017  路  34Comments  路  Source: openai/gym

Hi,

I am a beginner with gym. After I render CartPole

env = gym.make('CartPole-v0')
env.reset()
env.render()

Window is launched from Jupyter notebook but it hangs immediately. Then the notebook is dead. I am using Python 3.5.4 on OSX 10.11.6. What could be the problem here?

Thanks.

Most helpful comment

I was able to close the window using env.close(), but ideally it should close if I just hit the close icon on the window.

All 34 comments

I have exactly the same problem (not only in jupyter but in Python in general) with Python 3 3.6.2, OSX 10.13.

In my case, the window opens up and goes black. The training goes on in the background, but we don't get to see the simulation. I also keep getting pop ups of the simulation has hanged. Any fixes on this yet?
screenshot from 2017-11-25 20-37-23

I met the same problem from Jupyter Notebook with Python 2.7.13 and Mac 10.13.1.

I have the same problem with python 3.6.2 Mac 10.13.1 in Jupyter Notebook

Same issue python 3.6.4, macOS Sierra 10.12.6 in Jupyter

You can find a solution in #3. Just simply call env.render(close=True). It worked for me before but it did not work after I upgrading gym to version 0.9.6 which complained there is no close argument. It might work if you have a old version.

I checked the change log and the source code and found that close is not a argument anymore. Although the change log said that the window is now closable, I still can not close it. The only way is to close the whole env.

Same issue here, with Python 3.6.3 (miniconda) on OSX 10.13.3.
I have to terminate the python process in order to close the frozen render window.

Exception ignored in: <bound method SimpleImageViewer.__del__ of <gym.envs.classic_control.rendering.SimpleImageViewer object at 0x10c7d0898>>
Traceback (most recent call last):
  File "/Users/pcornice/devel/rl_with_keras_openai/part1/gym/gym/envs/classic_control/rendering.py", line 347, in __del__
  File "/Users/pcornice/devel/rl_with_keras_openai/part1/gym/gym/envs/classic_control/rendering.py", line 343, in close
  File "/usr/local/opt/pyenv/versions/miniconda3-latest/lib/python3.6/site-packages/pyglet/window/cocoa/__init__.py", line 281, in close
  File "/usr/local/opt/pyenv/versions/miniconda3-latest/lib/python3.6/site-packages/pyglet/window/__init__.py", line 772, in close
ImportError: sys.meta_path is None, Python is likely shutting down

Same problem, Ubuntu 16.04, on Python 3.6.3 (Anaconda).

I was able to close the window using env.close(), but ideally it should close if I just hit the close icon on the window.

Just want to confirm @samarthbhargav solution. Using env.close() works for Windows 10 on Python 3.5.0.

I don't think env.close() is a proper solution since you will clear all the intermediate values related to this env.

File "/home/shashank/.local/lib/python3.6/site-packages/gym/envs/classic_control/rendering.py", line 347, in __del__
File "/home/shashank/.local/lib/python3.6/site-packages/gym/envs/classic_control/rendering.py", line 343, in close
File "/home/shashank/.local/lib/python3.6/site-packages/pyglet/window/xlib/__init__.py", line 480, in close
File "/home/shashank/.local/lib/python3.6/site-packages/pyglet/gl/xlib.py", line 345, in destroy
File "/home/shashank/.local/lib/python3.6/site-packages/pyglet/gl/base.py", line 334, in destroy
File "/home/shashank/.local/lib/python3.6/site-packages/pyglet/gl/xlib.py", line 335, in detach
File "/home/shashank/.local/lib/python3.6/site-packages/pyglet/gl/lib.py", line 97, in errcheck
ImportError: sys.meta_path is None, Python is likely shutting down
is there any solution? please help

Same issues, Python 3.6.4 (brew installed) and macOS 10.13.3, whatever running in jupyter or console, the window just popped up and froze. So sad!

Reiterating:

import gym

env = gym.make('CartPole-v0')
env.reset()

for t in range(1000):
env.render()
env.step(env.action-space.sample())

Hangs requiring a force quit on both Python 3.5.4 and Python 3.6.4 on macOS 10.13.4, running in jupyter notebook. Killing python causes the kernel on all jupyter notebooks to die.
Also fails in terminal window with following error log and fixing the warning by adding
dtype=numpy.float32
and changing __init__ to include dtype box.py does not stop the hang.

python TFbc-31-OpenAIGymA.py
WARN: gym.spaces.Box autodetected dtype as . Please provide explicit dtype.
Traceback (most recent call last):
File "TFbc-31-OpenAIGymA.py", line 9, in
env.step(env.action-space.sample())
AttributeError: 'TimeLimit' object has no attribute 'action'
Exception ignored in: >
Traceback (most recent call last):
File "/Users/van/Dropbox/bin/miniconda3/envs/tfdeeplearning/lib/python3.5/site-packages/gym/envs/classic_control/rendering.py", line 143, in __del__
File "/Users/van/Dropbox/bin/miniconda3/envs/tfdeeplearning/lib/python3.5/site-packages/gym/envs/classic_control/rendering.py", line 62, in close
File "/Users/van/Dropbox/bin/miniconda3/envs/tfdeeplearning/lib/python3.5/site-packages/pyglet/window/cocoa/__init__.py", line 281, in close
File "/Users/van/Dropbox/bin/miniconda3/envs/tfdeeplearning/lib/python3.5/site-packages/pyglet/window/__init__.py", line 770, in close
File "", line 968, in _find_and_load
File "", line 953, in _find_and_load_unlocked
File "", line 887, in _find_spec
TypeError: 'NoneType' object is not iterable

Same problem.

On jupyter 1.0.0, python 3.6.4, gym 0.10.5, pyglet 1.2.4, I'm using the following code

import gym
env = gym.make('LunarLander-v2')
env.reset()
env.render()

This produces a frozen game window, which can't be closed. Even restarting the kernel doesn't work, only killing the process.

At the same time, I tried the random agent sample: here

It produces the following error message:

Exception ignored in: >
Traceback (most recent call last):
File "/home/lhk/anaconda3/lib/python3.6/site-packages/gym/envs/classic_control/rendering.py", line 143, in __del__
File "/home/lhk/anaconda3/lib/python3.6/site-packages/gym/envs/classic_control/rendering.py", line 62, in close
File "/home/lhk/anaconda3/lib/python3.6/site-packages/pyglet/window/xlib/__init__.py", line 474, in close
File "/home/lhk/anaconda3/lib/python3.6/site-packages/pyglet/gl/xlib.py", line 343, in destroy
File "/home/lhk/anaconda3/lib/python3.6/site-packages/pyglet/gl/base.py", line 333, in destroy
File "/home/lhk/anaconda3/lib/python3.6/site-packages/pyglet/gl/xlib.py", line 333, in detach
File "/home/lhk/anaconda3/lib/python3.6/site-packages/pyglet/gl/lib.py", line 96, in errcheck
ImportError: sys.meta_path is None, Python is likely shutting down

First time installing and using OpenAI Gym and the example failed.

git clone https://github.com/openai/gym.git
cd gym
pip install -e .
pip install -e '.[classic_control]'
cd examples/agents
python random_agent.py

Then I got

INFO: Making new env: CartPole-v0
/Users/zhanwenchen/Downloads/gym/gym/__init__.py:22: UserWarning: DEPRECATION WARNING: to improve load times, gym no longer automatically loads gym.spaces. Please run "import gym.spaces" to load gym.spaces on your own. This warning will turn into an error in a future version of gym.
  warnings.warn('DEPRECATION WARNING: to improve load times, gym no longer automatically loads gym.spaces. Please run "import gym.spaces" to load gym.spaces on your own. This warning will turn into an error in a future version of gym.')
WARN: gym.spaces.Box autodetected dtype as <class 'numpy.float32'>. Please provide explicit dtype.
/Users/zhanwenchen/Downloads/gym/gym/__init__.py:29: UserWarning: DEPRECATION WARNING: to improve load times, gym no longer automatically loads gym.wrappers. Please run "import gym.wrappers" to load gym.wrappers on your own. This warning will turn into an error in a future version of gym.
  warnings.warn('DEPRECATION WARNING: to improve load times, gym no longer automatically loads gym.wrappers. Please run "import gym.wrappers" to load gym.wrappers on your own. This warning will turn into an error in a future version of gym.')
INFO: Creating monitor directory /tmp/random-agent-results
INFO: Starting new video recorder writing to /tmp/random-agent-results/openaigym.video.0.21148.video000000.mp4
DEBUG: Capturing video frame: path=/tmp/random-agent-results/openaigym.video.0.21148.video000000.mp4
Traceback (most recent call last):
  File "random_agent.py", line 40, in <module>
    ob = env.reset()
  File "/Users/zhanwenchen/Downloads/gym/gym/wrappers/monitor.py", line 39, in reset
    self._after_reset(observation)
  File "/Users/zhanwenchen/Downloads/gym/gym/wrappers/monitor.py", line 193, in _after_reset
    self.reset_video_recorder()
  File "/Users/zhanwenchen/Downloads/gym/gym/wrappers/monitor.py", line 214, in reset_video_recorder
    self.video_recorder.capture_frame()
  File "/Users/zhanwenchen/Downloads/gym/gym/wrappers/monitoring/video_recorder.py", line 116, in capture_frame
    self._encode_image_frame(frame)
  File "/Users/zhanwenchen/Downloads/gym/gym/wrappers/monitoring/video_recorder.py", line 162, in _encode_image_frame
    self.encoder = ImageEncoder(self.path, frame.shape, self.frames_per_sec)
  File "/Users/zhanwenchen/Downloads/gym/gym/wrappers/monitoring/video_recorder.py", line 254, in __init__
    raise error.DependencyNotInstalled("""Found neither the ffmpeg nor avconv executables. On OS X, you can install ffmpeg via `brew install ffmpeg`. On most Ubuntu variants, `sudo apt-get install ffmpeg` should do it. On Ubuntu 14.04, however, you'll need to install avconv with `sudo apt-get install libav-tools`.""")
gym.error.DependencyNotInstalled: Found neither the ffmpeg nor avconv executables. On OS X, you can install ffmpeg via `brew install ffmpeg`. On most Ubuntu variants, `sudo apt-get install ffmpeg` should do it. On Ubuntu 14.04, however, you'll need to install avconv with `sudo apt-get install libav-tools`.
DEBUG: Writing training manifest file to /tmp/random-agent-results/openaigym.manifest.0.21148.manifest.json
INFO: Finished writing results. You can upload them to the scoreboard via gym.upload('/tmp/random-agent-results')
Exception ignored in: <bound method Viewer.__del__ of <gym.envs.classic_control.rendering.Viewer object at 0x10c8d68d0>>
Traceback (most recent call last):
  File "/Users/zhanwenchen/Downloads/gym/gym/envs/classic_control/rendering.py", line 143, in __del__
  File "/Users/zhanwenchen/Downloads/gym/gym/envs/classic_control/rendering.py", line 62, in close
  File "/Users/zhanwenchen/anaconda/lib/python3.6/site-packages/pyglet/window/cocoa/__init__.py", line 281, in close
  File "/Users/zhanwenchen/anaconda/lib/python3.6/site-packages/pyglet/window/__init__.py", line 770, in close
ImportError: sys.meta_path is None, Python is likely shutting down

In case others are wondering why their "env.close" isn't closing the window, be sure to call close on the original env from the gym.make call:

env_orig = gym.make('CartPole-v1') # <-- closes window
env = wrappers.Monitor(env_orig, ...) # <-- doesnt close window

Edit: @zhanwenchen Try installing the needed deps:

Found neither the ffmpeg nor avconv executables. On OS X, you can install ffmpeg via brew install ffmpeg. On most Ubuntu variants, sudo apt-get install ffmpeg should do it. On Ubuntu 14.04, however, you'll need to install avconv with sudo apt-get install libav-tools.

use the following X server
https://sourceforge.net/projects/vcxsrv/

No hang issues

I met the same problem with Python 3.6.4 on Mac High Sierra 10.13.5

Update:
Guys, I solved the issue by not running it through Jupyter Notebook. I run a .py script from Terminal and it works elegantly. I hope this can help.

@shuruiz that helped, thanks.

Any solution for Jupyter Notebook ?

@shaabhishek I haven't found any solution for Jupyter notebook yet. I think the problem occurs in Jupyter Kernel cuz jupyter notebook and python core use different visualization settings.

The same problem.
By running .py in termial, I can run the process, but I can not see the window then.

Running .py in terminal still reports the same error

Yeah the window freezes as well on the first frame when running a simulation on Terminal through a Conda environment. 'Application stopped responding'

When I run the CartPole-v0 example in Jupyter Notebook the simulator window opens and python3 then becomes unresponsive. I tried @shuruiz advice to save and run a .py instead of in Jupyter and it worked for me. I would like to find a solution for Jupyter. My laptop has two graphics cards, one supports OpenGL and I believe the other does not. Could this be the issue?

@chc8 Have you tried running the environment in the following way?

import gym

env = gym.make('CartPole-v0')
env.reset()

for _ in range(1000):
    env.render()
    env.step(env.action_space.sample()) #take a random action

env.close()

For some reason I haven't seen this documented but call env.close() at the end of your code and everything will run smoothly. Hope this helps!

@cyrilzakka I had everything except the env.close(). I added this call to close and technically python3 did not get hung up. The simulator ran, rendered nothing, then closed after a few seconds.

I did receive this 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."

I did receive this 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."

I have done exactly what @cyrilzakka showed and it worked for me.

import gym

env = gym.make('CartPole-v0')
env.reset()

while True:
    env.render()
    env.step(env.action_space.sample()) #take a random action

env.close()

But instead I used an infinite while loop so that it continues to render, to close the simulator, just close the terminal. Also, env.step() does a random action and in my environment, it just took the cart outside of the window. So try commenting env.step() and see whether it renders something or not.

Hi , I am using arm64 ubuntu with gym version- 0.10.9.
on running python example or some example created by me it gives the error:

$ python3 examples/agents/random_agent.py
INFO: Making new env: CartPole-v0
/home/nvidia/.local/lib/python3.6/site-packages/gym/envs/registration.py:14: PkgResourcesDeprecationWarning: Parameters to load are deprecated. Call .resolve and .require separately.
result = entry_point.load(False)
INFO: Clearing 2 monitor files from previous run (because force=True was provided)
INFO: Starting new video recorder writing to /tmp/random-agent-results/openaigym.video.0.20181.video000000.mp4
Segmentation fault (core dumped)

Also if i run my example as following:

import gym
env = gym.make('CartPole-v0')
for i_episode in range(20):
observation = env.reset()
for t in range(100):
env.render()
print(observation)
action = env.action_space.sample()
observation, reward, done, info = env.step(action)
if done:
print("Episode finished after {} timesteps".format(t+1))
break
env.close()

$ python3 cart.py
Segmentation fault (core dumped)

Thanks

Hey guys,
As I using an environment in anaconda to run python random_agent.py I solved part of this problem updating/installing ffmpegand av. Then, I did:

Installing _ffmpeg_:
conda install -c menpo ffmpeg

Installing _av_:
conda install -c conda-forge av
conda install -c conda-forge/label/gcc7 av
conda install -c conda-forge/label/cf201901 av

The problem now is that after few seconds the window for _CartPole-v0_ is closing and the error:

Exception ignored in: >

Anyone could help?

Thanks!

References:

  1. https://anaconda.org/menpo/ffmpeg
  2. https://anaconda.org/conda-forge/av

It looks like the original issue is due to not causing env.render() in a loop, the example on the docs page should be mostly accurate: https://gym.openai.com/. It could also check the return value of env.render() and exit if it's false (indicating that the user closed the rendering window).

Some of the other issues should be fixed on master. Please file new issues for any that are not currently fixed on master.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

RuofanKong picture RuofanKong  路  4Comments

Ettrig picture Ettrig  路  3Comments

hipoglucido picture hipoglucido  路  4Comments

RuofanKong picture RuofanKong  路  4Comments

RuofanKong picture RuofanKong  路  3Comments