Python - 用 gym 制作的脚本不适用于 Mac

Python - script made with gym does not work on Mac

我正在按照教程 here 学习深度强化学习(我对这个主题很陌生)。

当我尝试 运行 我的 Mac 预订下面的脚本时,它 returns 我遇到了导入错误 (ImportError: sys.meta_path is None, Python is likely shutting down)。 我尝试从 Mac 终端和 Pycharm 中对它进行 运行,结果相同。 我也尝试了其他深度学习脚本,错误是一样的(它似乎与这个脚本没有特别相关)。

你能帮我理解我哪里错了吗?

脚本:

# Import the gym module
import gym

# Create a breakout environment
env = gym.make('BreakoutDeterministic-v4')
# Reset it, returns the starting frame
frame = env.reset()
# Render
env.render()

is_done = False
while not is_done:
  # Perform a random action, returns the new frame, reward and whether the game is over
  frame, reward, is_done, _ = env.step(env.action_space.sample())
  # Render
  env.render()

错误如下:

/usr/local/bin/python3.6 /Users/marcogdepinto/PycharmProjects/PlayPong/pong.py
2018-05-12 18:58:11.915 Python[567:12594] ApplePersistenceIgnoreState: Existing state will not be touched. New state will be written to (null)
Exception ignored in: <bound method SimpleImageViewer.__del__ of <gym.envs.classic_control.rendering.SimpleImageViewer object at 0x10b65bc88>>
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/gym/envs/classic_control/rendering.py", line 347, in __del__
  File "/usr/local/lib/python3.6/site-packages/gym/envs/classic_control/rendering.py", line 343, in close
  File "/usr/local/lib/python3.6/site-packages/pyglet/window/cocoa/__init__.py", line 281, in close
  File "/usr/local/lib/python3.6/site-packages/pyglet/window/__init__.py", line 770, in close
ImportError: sys.meta_path is None, Python is likely shutting down

Process finished with exit code 0

"Try adding env.close() at the end of your programs, in my scenario, it works nice"

src

只需在程序末尾添加以下行,在 gym 模块的 GC 期间会引发错误。

try:
    del env
except ImportError:
    pass