Gym (OpenAI Gym)

0.26.2 · deprecated · verified Sat Apr 11

Gym (formerly OpenAI Gym) is a Python library that provided a universal API for developing and comparing reinforcement learning (RL) algorithms across a diverse collection of environments. While it was historically the standard for RL environments, the `gym` library is no longer actively maintained. All future development and support have transitioned to its successor, `gymnasium`, a drop-in replacement. The last major release of `gym` was version 0.26.2, released in October 2022, which introduced significant breaking API changes.

Warnings

Install

Imports

Quickstart

This example demonstrates how to create a CartPole-v1 environment, reset it with a seed, take random actions, and handle the new 5-tuple return value from `step()` and 2-tuple from `reset()` in Gym 0.26.x+. The environment is rendered to a human-viewable window.

import gym

env = gym.make("CartPole-v1", render_mode="human")

# Reset returns (observation, info) in 0.26.x+
observation, info = env.reset(seed=42)

for _ in range(1000):
    action = env.action_space.sample()  # Agent selects an action
    # Step returns (observation, reward, terminated, truncated, info) in 0.26.x+
    observation, reward, terminated, truncated, info = env.step(action)

    if terminated or truncated:
        print(f"Episode finished after {_+1} timesteps.")
        observation, info = env.reset(seed=42) # Reset for a new episode

env.close()

view raw JSON →