PettingZoo
PettingZoo is a Python library providing a standardized API for multi-agent reinforcement learning (MARL) environments, analogous to Gymnasium for single-agent RL. It offers a wide variety of reference environments and utilities, supporting both sequential (AEC) and simultaneous (Parallel) action paradigms. Currently at version 1.25.0, the library is actively maintained with frequent updates to ensure compatibility with recent Python and Gymnasium versions.
Common errors
-
TypeError: reset() got an unexpected keyword argument 'return_info'
cause Attempting to call `env.reset(return_info=True/False)` in PettingZoo versions that have aligned with Gymnasium API, where `return_info` is no longer an argument and info is always returned.fixRemove the `return_info` argument from `env.reset()`. The `info` dictionary is now always returned implicitly: `observation, info = env.reset()`. -
AttributeError: 'PettingZooEnv' object has no attribute 'seed'
cause Attempting to call `env.seed()` on a PettingZoo environment in versions where this method has been deprecated and removed.fixPass the `seed` argument directly to `env.reset()` instead: `env.reset(seed=123)`. -
AssertionError: Your environment must inherit from the gym.Env class
cause Using `stable_baselines3.common.env_checker.check_env` directly on a PettingZoo environment, which implements `AECEnv` or `ParallelEnv` not `gym.Env`.fixPettingZoo environments are not direct `gym.Env` subclasses. Use `pettingzoo.test.api_test` or `pettingzoo.test.parallel_api_test` to validate PettingZoo API compliance. For integration with `stable_baselines3`, typically a wrapper (like those from `SuperSuit`) is needed to adapt the PettingZoo API. -
ERROR: This environment does not support render_mode='human'
cause The specific environment or its underlying rendering backend does not support the 'human' render_mode, or necessary display dependencies are missing.fixCheck the environment's documentation for supported `render_mode` options. Many environments support `rgb_array` which returns pixel data. Ensure all necessary rendering dependencies (e.g., `pygame`) are installed, potentially via `pip install 'pettingzoo[atari]'` for specific environment families. -
KeyError: 'agent_id' during agent interaction loop or observation/action space lookup.
cause Trying to access an observation or action space for an agent that is no longer active or was never part of `env.agents` or `env.possible_agents`.fixAlways check `if agent in env.agents:` before requesting actions or observations for an agent, especially in environments with variable numbers of agents (e.g., agents can be removed upon 'termination' or 'truncation').
Warnings
- breaking Python 3.8 is no longer supported starting from PettingZoo 1.25.0. Ensure your Python environment is 3.9 or higher.
- deprecated The `pettingzoo.mpe` environments are being transferred to a new dedicated `MPE2` package and will be removed from PettingZoo in a future release.
- breaking The `env.seed()` method has been removed to align with Gymnasium API. Seeding is now handled by the `env.reset()` method.
- breaking The `return_info` argument has been removed from `env.reset()`. Info is now always returned as the second element of the tuple, consistent with Gymnasium.
- breaking The `agent_selector` class was renamed to `AgentSelector` (capitalization change).
- gotcha Windows support is not officially maintained, though PRs related to Windows are accepted. Users on Windows may encounter installation or runtime issues, especially with environment dependencies.
- breaking PettingZoo 1.22.4 was yanked from PyPI due to breaking API changes that were accidentally included. Users on this specific version should upgrade.
Install
-
pip install pettingzoo -
pip install 'pettingzoo[all]' # Install all environment dependencies -
pip install 'pettingzoo[butterfly]' # Install dependencies for a specific family (e.g., Butterfly) -
pip install supersuit # Common companion library for wrappers/preprocessing
Imports
- Environment
from pettingzoo.butterfly import pistonball_v6 env = pistonball_v6.env()
- AECEnv, ParallelEnv
from pettingzoo import AECEnv, ParallelEnv
from pettingzoo.utils.env import AECEnv, ParallelEnv
- wrappers
from pettingzoo.utils import wrappers
- AgentSelector
from pettingzoo.utils import AgentSelector
from pettingzoo.utils.agent_selector import AgentSelector
Quickstart
from pettingzoo.butterfly import pistonball_v6
env = pistonball_v6.env(render_mode="human")
env.reset(seed=42)
for agent in env.agent_iter():
observation, reward, terminated, truncated, info = env.last()
if terminated or truncated:
action = None
else:
# In a real scenario, this would be your policy's action
action = env.action_space(agent).sample()
env.step(action)
env.close()