Part 1 – Registering a custom Gym environment

In this tutorial, we will create and register a minimal gym environment. Please read the introduction before starting this tutorial.

First of all, let’s understand what is a Gym environment exactly. A Gym environment contains all the necessary functionalities to that an agent can interact with it. Basically, it is a class with 4 methods:

  • __init__: initialization function of the class
  • step: it takes an action argument and returns a list of 4 items: the next state, the reward of the current state, a boolean representing whether the current episode of our model is done and some additional info on our problem
  • reset: resets the state and other variables of the environment to the start state
  • render: displays the environment graphically

All Gym environments come in a PIP package with the following file structure.


Let’s explain each items one by one.

This is the main directory where the environments live.

This is a short description of the environment.

# gym_foo
gym_foo is a minimal Gym environment used to illustrate how to create a custom Gym environment.

## Installation and environment registration
pip install gym
pip install -e .
## Test your installation

This is the PIP installation file. It describes the name, version and required dependencies of our environment.

from setuptools import setup


This file defines the the environment version name and entry points. Make sure you respect the spelling.

from gym.envs.registration import register


This file contains the following:

from gym_foo.envs.foo_env import FooEnv

This is where the environment class and its 4 methods are described. Since this is a minimal working example, these methods will be left empty for now.

import gym
from gym import error, spaces, utils
from gym.utils import seeding

class FooEnv(gym.Env):
    metadata = {'render.modes': ['human']}

    def __init__(self):
    def step(self, action):
    def reset(self):
    def render(self, mode='human', close=False):

We are now ready to install and register the environment. Go back to the initial directory gym-foo and run this command to install the PIP package.

pip install -e .

The -e option means that the package is installed in “editable” mode. This means that the package is installed locally and that any changes to the original package will reflect directly in your environment.

Finally, we can test that everything is working fine by creating one last file:

import gym
import gym_foo

env = gym.make('foo-v0')

If you are able to run this without error, congratulations you have successfully registered your first Gym environment!

The code for this tutorial can be found on GitHub.

Note that you can list all the environments registered in Gym using the following Python code.

from gym import envs
import gym_foo

envids = [ for spec in envs.registry.all()]
for envid in sorted(envids):

In the next article, we will implement the 4 methods to create a simple Tic Tac Toe environment.

Leave a Reply