I’m trying to set up a custom OpenAI Gym environment on my Linux machine. So far, I’ve managed to follow the guide and complete most steps, including executing pip install -e .. However, I receive an error when I attempt to create the environment with env = gym.make('CustomEnv-v1').
from setuptools import setup
setup(name='myEnvironment',
version='0.1.0',
install_requires=['gym']
)
For the main init.py file, I have:
from gym.envs.registration import register
register(
id='myEnvironment',
entry_point='myEnvironment.envs:customEnv',
)
In the envs/init.py file, I wrote:
from myEnvironment.envs.customEnv import customEnv
The installation through pip worked fine, and the package was successfully added. But when I run the following code:
import numpy as np
import pandas as pd
import time
import gym
import myEnvironment
env = gym.make('myEnvironment')
I encounter this error: gym.error.Error: Attempted to register malformed environment ID: myEnvironment. (Currently all IDs must be of the form ^(?:[\w:-]+\/)?([\w:.-]+)-v(\d+)$.)
Could someone guide me on what I might be missing in the environment registration process?
The error’s pretty straightforward - your environment ID doesn’t follow OpenAI Gym’s naming rules. Gym needs environment IDs to end with -v plus a version number. You’re registering with id='myEnvironment' in your __init__.py but calling gym.make('myEnvironment'). Change your registration to id='CustomEnv-v0' or id='MyEnvironment-v0'. I hit this same issue when I built my first custom environment. The version suffix isn’t optional - it’s required. Once you fix the ID format, run pip install -e . again so the changes stick.
you’ve got the naming wrong. you’re calling gym.make(‘myEnvironment’) but gym needs the -v0 or -v1 suffix. also, your entry_point should probably be ‘myEnvironment.envs:CustomEnv’ not ‘myEnvironment.envs:customEnv’ unless your class is actually lowercase. double check that your class name in customEnv.py matches exactly.
I see the problem - your registered ID doesn’t match what you’re calling in gym.make. You registered with id='myEnvironment' but you’re trying to create CustomEnv-v1. Pick one approach and stick with it. Either change your registration to id='CustomEnv-v1' to match your gym.make call, or switch the gym.make to use myEnvironment-v0 and update your registration accordingly. Just remember Gym needs that -v version number at the end. This usually happens when you mix code from different tutorials.
Two things are breaking your registration. First, you’re calling gym.make('myEnvironment') but registered it as id='myEnvironment' without the required version suffix. Gym needs the -v(digit) format, so change your registration to id='myEnvironment-v0' and call gym.make('myEnvironment-v0'). Second, your entry point myEnvironment.envs:customEnv doesn’t match your actual class name. If your class in customEnv.py is called CustomEnv, fix the entry point to match. I hit these same naming issues building trading environments last year - it’s always the naming that gets you.