Skip to main content
 首页 » 编程设计

Python之用 gym 制作的脚本在 Mac 上不起作用

2024年09月07日1252php

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

当我尝试在我的 Macbook 上运行下面的脚本时,它返回一个导入错误(ImportError: sys.meta_path is None, Python is likely shutdown)。 我尝试从 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