A standard API for reinforcement learning and a diverse set of reference environments (formerly Gym).
—
The registration system manages environment discovery and instantiation through string IDs. It provides a global registry where environments can be registered and created by name, enabling consistent environment access across different codebases.
Functions for creating environment instances from registered IDs.
def make(id: str | EnvSpec, max_episode_steps: int | None = None,
disable_env_checker: bool | None = None, **kwargs) -> Env:
"""
Create an environment instance from a registered ID.
Args:
id: Environment ID (e.g., 'CartPole-v1') or EnvSpec instance
max_episode_steps: Override max episode steps (use -1 to disable TimeLimit)
disable_env_checker: Whether to disable environment checker
**kwargs: Additional arguments passed to environment constructor
Returns:
Environment instance
Raises:
UnregisteredEnv: If environment ID is not registered
DependencyNotInstalled: If required dependencies are missing
"""
def make_vec(id: str | EnvSpec, num_envs: int = 1,
vectorization_mode: VectorizeMode | str | None = None,
vector_kwargs: dict[str, Any] | None = None,
wrappers: Sequence[Callable[[Env], Wrapper]] | None = None,
**kwargs) -> VectorEnv:
"""
Create a vectorized environment instance.
Args:
id: Environment ID to vectorize or EnvSpec instance
num_envs: Number of parallel environments (default: 1)
vectorization_mode: VectorizeMode.SYNC or VectorizeMode.ASYNC
vector_kwargs: Additional arguments for vector environment
wrappers: Sequence of wrapper functions to apply
**kwargs: Additional arguments passed to environment constructor
Returns:
Vectorized environment instance
"""Functions for registering new environments with the global registry.
def register(id: str, entry_point: EnvCreator | str | None = None,
reward_threshold: float | None = None, nondeterministic: bool = False,
max_episode_steps: int | None = None, order_enforce: bool = True,
disable_env_checker: bool = False,
additional_wrappers: tuple[WrapperSpec, ...] = (),
vector_entry_point: VectorEnvCreator | str | None = None,
kwargs: dict | None = None) -> None:
"""
Register a new environment with the global registry.
Args:
id: Unique environment ID (e.g., 'MyEnv-v0')
entry_point: Import path to environment class or callable creating the environment
reward_threshold: Reward threshold for completing the environment
nondeterministic: Whether environment is nondeterministic
max_episode_steps: Maximum steps per episode before truncation
order_enforce: Whether to enforce reset before step order
disable_env_checker: Whether to disable environment checker
additional_wrappers: Tuple of wrapper specs to apply
vector_entry_point: Entry point for vectorized environment
kwargs: Additional keyword arguments for environment
"""
def register_envs(env_module: ModuleType) -> None:
"""
No-op function for IDE compatibility when importing modules.
Args:
env_module: Module containing environment registrations
"""Functions for exploring registered environments.
def spec(id: str) -> EnvSpec:
"""
Get the specification for a registered environment.
Args:
id: Environment ID
Returns:
Environment specification object
Raises:
UnregisteredEnv: If environment ID is not registered
"""
def pprint_registry(print_registry: dict[str, EnvSpec] = registry, *,
num_cols: int = 3, exclude_namespaces: list[str] | None = None,
disable_print: bool = False) -> str | None:
"""
Pretty print all registered environments.
Args:
print_registry: Environment registry to print (default: global registry)
num_cols: Number of columns for display
exclude_namespaces: Namespaces to exclude from output
disable_print: If True, return string instead of printing
Returns:
Formatted string if disable_print=True, otherwise None
"""
registry: dict[str, EnvSpec]
"""Global environment registry dictionary mapping IDs to specs."""Classes that define environment and wrapper specifications.
class EnvSpec:
"""
Environment specification containing registration metadata.
Attributes:
id: Environment ID
entry_point: Import path to environment class or callable
reward_threshold: Reward threshold for considering environment solved
nondeterministic: Whether environment is nondeterministic
max_episode_steps: Maximum steps per episode
order_enforce: Whether to enforce reset before step order
disable_env_checker: Whether to disable environment checker
kwargs: Additional keyword arguments for environment
namespace: Parsed namespace from ID (post-init)
name: Parsed name from ID (post-init)
version: Parsed version from ID (post-init)
additional_wrappers: Tuple of additional wrapper specs
vector_entry_point: Entry point for vectorized environment
"""
class WrapperSpec:
"""
Wrapper specification for automatic wrapper application.
Attributes:
name: Wrapper name
entry_point: Import path to wrapper class
kwargs: Keyword arguments for wrapper constructor
"""
class VectorizeMode(Enum):
"""
Enumeration of vectorization modes.
Values:
SYNC: Synchronous vectorization
ASYNC: Asynchronous vectorization
VECTOR_ENTRY_POINT: Use custom vector entry point
"""
SYNC = "sync"
ASYNC = "async"
VECTOR_ENTRY_POINT = "vector_entry_point"import gymnasium as gym
# Create a standard environment
env = gym.make('CartPole-v1')
# Create with custom parameters
env = gym.make('CartPole-v1', render_mode='human')
# Create vectorized environments
vec_env = gym.make_vec('CartPole-v1', num_envs=4)
# Get environment specification
spec = gym.spec('CartPole-v1')
print(f"Max episode steps: {spec.max_episode_steps}")
print(f"Reward threshold: {spec.reward_threshold}")from gymnasium.envs.registration import register
# Register a simple custom environment
register(
id='MyCustomEnv-v0',
entry_point='mypackage.envs:MyCustomEnv',
max_episode_steps=1000,
reward_threshold=500.0
)
# Register with additional wrappers
register(
id='MyWrappedEnv-v0',
entry_point='mypackage.envs:MyEnv',
additional_wrappers=[
{'name': 'TimeLimit', 'kwargs': {'max_episode_steps': 200}},
{'name': 'FlattenObservation', 'kwargs': {}}
]
)
# Now you can create instances
env = gym.make('MyCustomEnv-v0')
wrapped_env = gym.make('MyWrappedEnv-v0')import gymnasium as gym
# Print all registered environments
gym.pprint_registry()
# Get specific environment spec
cartpole_spec = gym.spec('CartPole-v1')
print(f"Entry point: {cartpole_spec.entry_point}")
print(f"Max steps: {cartpole_spec.max_episode_steps}")
# Check if environment is registered
try:
spec = gym.spec('NonExistentEnv-v0')
except gym.error.UnregisteredEnv:
print("Environment not found")# Environments can be organized in namespaces
# Example: 'ALE/Breakout-v5' is in the 'ALE' namespace
# Create namespaced environment
atari_env = gym.make('ALE/Breakout-v5')
# Register in custom namespace
register(
id='MyNamespace/CustomEnv-v0',
entry_point='mypackage:MyEnv'
)
custom_env = gym.make('MyNamespace/CustomEnv-v0')# For packaging environments in separate packages
# In setup.py or pyproject.toml:
setup(
name='my-gym-envs',
entry_points={
'gymnasium.envs': [
'my_namespace:my_gym_envs.registration:register_envs'
]
}
)
# In my_gym_envs/registration.py:
def register_envs():
register(
id='MyNamespace/Env1-v0',
entry_point='my_gym_envs.envs:Env1'
)
register(
id='MyNamespace/Env2-v0',
entry_point='my_gym_envs.envs:Env2'
)
# Environments are auto-registered when package is installedInstall with Tessl CLI
npx tessl i tessl/pypi-gymnasium