import gym
env=gym.make("LunarLander-v2",render_mode='human')
env.action_space.seed(42)
observation,info=env.reset(seed=42)
for _ in range(1000):
observation,reward,terminated,truncated,info=env.step(env.action_space.sample())
if terminated or truncated:
observation,info=env.reset()
env.close()
上述程序对于gym进行基本测试。结果运行的时间却报错了,具体报错信息如下:
D:\Python39\python.exe F:\myDQN\gymTest.py
Traceback (most recent call last):
File “F:\myDQN\gymTest.py”, line 2, in <module>
env=gym.make(“LunarLander-v2”,render_mode=’human’)
File “D:\Python39\lib\site-packages\gym\envs\registration.py”, line 676, in make
return registry.make(id, kwargs) File “D:\Python39\lib\site-packages\gym\envs\registration.py”, line 520, in make return spec.make(kwargs)
File “D:\Python39\lib\site-packages\gym\envs\registration.py”, line 139, in make
cls = load(self.entry_point)
File “D:\Python39\lib\site-packages\gym\envs\registration.py”, line 56, in load
fn = getattr(mod, attr_name)
AttributeError: module ‘gym.envs.box2d’ has no attribute ‘LunarLander’
Process finished with exit code 1
经过搜索发现,https://blog.csdn.net/weixin_44480968/article/details/124743355 这篇文章中提到需要安装两个软件包 swig和box2d
pip install swig -i http://pypi.douban.com/simple –trusted-host pypi.douban.com
pip install box2d box2d-kengz -i http://pypi.douban.com/simple –trusted-host pypi.douban.com
通过上述命令可以顺利安装,但是重新运行上面的基础程序出现了新的报错信息:
D:\Python39\python.exe F:\myDQN\gymTest.py
Traceback (most recent call last):
File “F:\myDQN\gymTest.py”, line 2, in <module>
env=gym.make(“LunarLander-v2”,render_mode=’human’)
File “D:\Python39\lib\site-packages\gym\envs\registration.py”, line 676, in make
return registry.make(id, kwargs) File “D:\Python39\lib\site-packages\gym\envs\registration.py”, line 520, in make return spec.make(kwargs)
File “D:\Python39\lib\site-packages\gym\envs\registration.py”, line 139, in make
cls = load(self.entry_point)
File “D:\Python39\lib\site-packages\gym\envs\registration.py”, line 55, in load
mod = importlib.import_module(mod_name)
File “D:\Python39\lib\importlib__init__.py”, line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File “<frozen importlib._bootstrap>”, line 1030, in _gcd_import
File “<frozen importlib._bootstrap>”, line 1007, in _find_and_load
File “<frozen importlib._bootstrap>”, line 986, in _find_and_load_unlocked
File “<frozen importlib._bootstrap>”, line 680, in _load_unlocked
File “<frozen importlib._bootstrap_external>”, line 850, in exec_module
File “<frozen importlib._bootstrap>”, line 228, in _call_with_frames_removed
File “D:\Python39\lib\site-packages\gym\envs\box2d__init__.py”, line 2, in <module>
import Box2D
File “D:\Python39\lib\site-packages\Box2D__init__.py”, line 20, in <module>
from .Box2D import *
File “D:\Python39\lib\site-packages\Box2D\Box2D.py”, line 435, in <module>
_Box2D.RAND_LIMIT_swigconstant(_Box2D)
AttributeError: module ‘_Box2D’ has no attribute ‘RAND_LIMIT_swigconstant’
Process finished with exit code 1
好几篇文章都提到需要安装pip install box2d box2d-kengz 而实际上这个命令已经在本文中运行安装了,所以应该不是这个问题
后来找到一篇文章说是需要 pip install box2d-py,遂运行安装。
然后重新运行最开始的gym基础程序,发现又出现了新的报错信息:
D:\Python39\python.exe F:\myDQN\gymTest.py
Traceback (most recent call last):
File “F:\myDQN\gymTest.py”, line 2, in <module>
env=gym.make(“LunarLander-v2”,render_mode=’human’)
File “D:\Python39\lib\site-packages\gym\envs\registration.py”, line 676, in make
return registry.make(id, kwargs) File “D:\Python39\lib\site-packages\gym\envs\registration.py”, line 520, in make return spec.make(kwargs)
File “D:\Python39\lib\site-packages\gym\envs\registration.py”, line 140, in make
env = cls(**_kwargs)
TypeError: __init__() got an unexpected keyword argument ‘render_mode’
Process finished with exit code 1
针对这段基础代码错误百出,重新在网上搜索相关内容,发现了如下源代码:
import gym
env = gym.make("LunarLander-v2")
env.reset()
env.action_space.seed(42)
observation, info = env.reset(seed=42, return_info=True)
for _ in range(1000):
env.render(mode='human')
observation, reward, done, info = env.step(env.action_space.sample())
if done:
observation, info = env.reset(return_info=True)
env.close()
这段代码可以顺利运行。这很可能是gym版本之间的差异造成的吧