Gym: How do you unregister gym environments?

Created on 20 Sep 2018  路  9Comments  路  Source: openai/gym

Is there a command to unregister existing registered (custom-made) gyms given a specific id?

Most helpful comment

@christopherhesse For what it's worth: I just used this for my project.
I started creating the environment in a Jupyter notebook and then used the code to quickly unregister and re-register the environment so I wouldn't have to restart the Jupyter kernel.

Without the del ... I get a boring Error: Cannot re-register id: MyEnv-v0 until I restart. The code allows me to modify the environment and run the cell repeatedly without issues.

class MyEnv(gym.core.Env):
    # here is my env code
    pass

# delete if it's registered
env_name = 'MyEnv-v0'
if env_name in gym.envs.registry.env_specs:
    del gym.envs.registry.env_specs[env_name]

# register the environment so we can play with it
gym.register(
    id=env_name,
    entry_point=MyEnv,
    max_episode_steps=999,
    reward_threshold=90.0,
)

PS: Obviously, I'm not sure what the OP was doing with it
PPS: Not sure if a dedicated, "nice" unregister API is needed by many people, but having a (this) simple way of unregistering is helpful for quick prototyping.

All 9 comments

del gym.registry.env_specs[<env-id>] should do the trick

it does not work though, it says gym does not have an attribute named registry

Ok, so to fix this error message I need to write gym.envs.registry.....

However the issue remains that sometimes the command does not recognize the existance of this env (although it was just registered and used) and even when it accepts its existence and deletes it, I still can't re-register another environment with the same name. Is there a more powerful way to de-register envs? Like deleting some file/folder in the dependencies?

Could you explain the reason you want to de-register an environment?

Closing due to lack of activity.

@christopherhesse For what it's worth: I just used this for my project.
I started creating the environment in a Jupyter notebook and then used the code to quickly unregister and re-register the environment so I wouldn't have to restart the Jupyter kernel.

Without the del ... I get a boring Error: Cannot re-register id: MyEnv-v0 until I restart. The code allows me to modify the environment and run the cell repeatedly without issues.

class MyEnv(gym.core.Env):
    # here is my env code
    pass

# delete if it's registered
env_name = 'MyEnv-v0'
if env_name in gym.envs.registry.env_specs:
    del gym.envs.registry.env_specs[env_name]

# register the environment so we can play with it
gym.register(
    id=env_name,
    entry_point=MyEnv,
    max_episode_steps=999,
    reward_threshold=90.0,
)

PS: Obviously, I'm not sure what the OP was doing with it
PPS: Not sure if a dedicated, "nice" unregister API is needed by many people, but having a (this) simple way of unregistering is helpful for quick prototyping.

where do you add those lines? in __init__.py?

where do you add those lines? in init.py?

I was executing it inside a Jupyter notebook. There was a cell that allowed me to unregister and re-register quickly

Have you checked all your pip packages? I ran into the same issue and it came out that I pip installed the environment twice at 2 different root dirs. Simply deleting them and pip install again fixed it for me.

Using gym.envs.registration.registry.env_specs instead of gym.envs.registry.env_specs worked for me

env_dict = gym.envs.registration.registry.env_specs.copy()
for env in env_dict:
    if env_name in env:
         print("Remove {} from registry".format(env))
         del gym.envs.registration.registry.env_specs[env]
Was this page helpful?
0 / 5 - 0 ratings

Related issues

Kallin picture Kallin  路  4Comments

RuofanKong picture RuofanKong  路  4Comments

david8373 picture david8373  路  4Comments

hipoglucido picture hipoglucido  路  4Comments

tornadomeet picture tornadomeet  路  4Comments