Python 错误显示 pygame 和gymnasium [classic-control] 未安装,但两者均已安装

问题描述 投票:0回答:2

我刚刚开始学习

OpenAI gymnasium
并从
CartPole-v1
开始。
作为新手,我正在学习 YouTube 教程;
video:https://www.youtube.com/watch?v=Mut_u40Sqz4&t=2076s

(我已经到了1:08:22)而且我还想看一下游戏的窗口。
然而,在运行代码时,Python 抛出了这个错误:

Traceback (most recent call last):   File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/gymnasium/envs/classic_control/cartpole.py", line 223, in render
    import pygame ModuleNotFoundError: No module named 'pygame'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):   File "/Users/artemnikoan/Documents/CartPole-v1.py", line 9, in <module>
    state = env.reset()   File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/gymnasium/wrappers/time_limit.py", line 75, in reset
    return self.env.reset(**kwargs)   File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/gymnasium/wrappers/order_enforcing.py", line 61, in reset
    return self.env.reset(**kwargs)   File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/gymnasium/wrappers/env_checker.py", line 57, in reset
    return env_reset_passive_checker(self.env, **kwargs)   File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/gymnasium/utils/passive_env_checker.py", line 186, in env_reset_passive_checker
    result = env.reset(**kwargs)   File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/gymnasium/envs/classic_control/cartpole.py", line 209, in reset
    self.render()   File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/gymnasium/envs/classic_control/cartpole.py", line 226, in render
    raise DependencyNotInstalled( gymnasium.error.DependencyNotInstalled: pygame is not installed, run `pip install gymnasium[classic-control]`

我确定安装了

pygame
gymnasium classic-control
,但仍然不起作用。

这是代码:

import os
import gymnasium as gym
from stable_baselines3 import PPO
from stable_baselines3.common.vec_env import DummyVecEnv
from stable_baselines3.common.evaluation import evaluate_policy
env=gym.make('CartPole-v1',render_mode='human')
episodes = 5
for episode in range(1, episodes+1):
    state = env.reset()
    done = False
    score = 0 
    while not done:
        env.render()
        action = env.action_space.sample()
        n_state, reward, done, truncated, info = env.step(action)
        score+=reward
    print('Episode:{} Score:{}'.format(episode, score))
env.close()
log_path=os.path.join('Training', 'Logs')
env = gym.make(environment_name)
env = DummyVecEnv([lambda: env])
model = PPO('MlpPolicy', env, verbose = 1)
model.learn(total_timesteps=10000)
PPO_path = os.path.join('Training', 'Saved Models', 'PPO_model')
model.save(PPO_path)
model = PPO.load(PPO_path, env=env)
evaluate_policy(model, env, n_eval_episodes=10, render=True)

我正在使用

MAC PC
来处理
IDLE

我安装了
Pytorch
和所有其他必需的模块。

可能是什么原因?

python reinforcement-learning openai-gym
2个回答
1
投票

您尚未安装

pygame
。错误消息已经表明您必须执行以下操作:
pip install gymnasium[classic-control]

考虑到 OG 中提供的视频是 2021 年 6 月 6 日开始的,Gym 版本(不是 Gymnasium)必须为 0.18.3 或更低版本。在较新的 Gym 版本中,语法发生了很大变化,这会导致许多错误,就像您所经历的那样。可以肯定的是,提供的视频不使用 Gymnasium

因此,我还建议将 Gym 降级到 <= 0.18.3. I could not find out from the video what version they used, but it must be in aforementioned range. Or, you could make yourself familiar with Gymnasium 版本或较新版本的 Gym 来重写部分代码(因为 Stable Baselines 3 确实适用于任一版本)。

可能有帮助:


0
投票

尝试

pip install 'gymnasium[classic-control]'
如果您使用 zsh / macOS

© www.soinside.com 2019 - 2024. All rights reserved.