Skip to content

Registration API

Importing gym_classics2 automatically registers all bundled environments with Gymnasium. Existing register() calls are harmless; repeated calls for the same backend do nothing. Use register(backend="gym") to additionally register with legacy Gym when it is installed.

gym_classics2.register

register(backend='gymnasium')

Register all bundled environments with Gymnasium or legacy Gym.

Importing gym_classics2 automatically registers with Gymnasium. Explicit calls remain supported; repeated calls for a backend do nothing.

Parameters:

Name Type Description Default
backend

Registry to use. "gymnasium" is the default and recommended value; "gym" supports legacy installations.

'gymnasium'

Raises:

Type Description
AssertionError

If backend is neither "gymnasium" nor "gym".

Example

import gymnasium as gym import gym_classics2 env = gym.make("ClassicGridworld-v1")

Source code in gym_classics2/__init__.py
def register(backend='gymnasium'):
    """Register all bundled environments with Gymnasium or legacy Gym.

    Importing ``gym_classics2`` automatically registers with Gymnasium.
    Explicit calls remain supported; repeated calls for a backend do nothing.

    Args:
        backend: Registry to use. ``"gymnasium"`` is the default and recommended
            value; ``"gym"`` supports legacy installations.

    Raises:
        AssertionError: If *backend* is neither ``"gymnasium"`` nor ``"gym"``.

    Example:
        >>> import gymnasium as gym
        >>> import gym_classics2
        >>> env = gym.make("ClassicGridworld-v1")
    """
    assert backend in {'gym', 'gymnasium'}
    if backend in _registered_backends:
        return

    if backend == 'gym':
        import gym
        register = gym.envs.register
    elif backend == 'gymnasium':
        import gymnasium
        register = gymnasium.register

    for kwargs in _registry:
        register(**kwargs)

    _registered_backends.add(backend)