Search code examples
pythonreinforcement-learningopenai-gym

Python error showing pygame and gymnasium [classic-control] not installed, however both have been


I have just started learning OpenAI gymnasium and started with CartPole-v1.
Being new I was following a YouTube tutorial;
video:https://www.youtube.com/watch?v=Mut_u40Sqz4&t=2076s
(I am up to 1:08:22) and I also wanted to see a window of the game.
However when running the code, Python threw this error:

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]`

I made sure pygame and gymnasium classic-control are installed, but it still didn't work.

Here is the code:

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)

I am working on MAC PC with using IDLE.
I installed Pytorch and every other required module.

What may be the cause?


Solution

  • You have not installed pygame. The error message already shows that you have to do: pip install gymnasium[classic-control].

    Considering the video provided in OG was from 6th of June 2021, the Gym version (not Gymnasium) must have been 0.18.3 or lower. The syntax has changed a lot throughout the newer Gym versions, which causes many errors just like you have experienced. One thing for sure, the video provided does not use Gymnasium.

    I thus also suggest either to downgrade Gym to a version <= 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 or newer versions of Gym to rewrite parts of the code (since Stable Baselines 3 does work with either version).

    Might help: