Farama-Foundation / Gymnasium

An API standard for single-agent reinforcement learning environments, with popular reference environments and related utilities (formerly Gym)
https://gymnasium.farama.org
MIT License
7.43k stars 839 forks source link

How does one call custom environments from outside the environment folder? #764

Closed Deepakgthomas closed 8 months ago

Deepakgthomas commented 1 year ago

Question

Can I access a custom gymnasium environment from outside its directory? This is how I call my gym environment -

import gym_examples
import gym
env = gym.make('gym_examples/GridWorld-v0')
obs = env.reset()
print("obs = ", obs)

I have attached a picture of the structure of my folder. Basically, I am following the instructions given over here - https://gymnasium.farama.org/tutorials/gymnasium_basics/environment_creation/

I already tried this -

import gym_examples
import gym
env = gym.make('my_foo_folder/gym_examples/GridWorld-v0')
obs = env.reset()
print("obs = ", obs)
gym.error.Error: Malformed environment ID: my_foo_folder/gym_examples/GridWorld-v0.(Currently all IDs must be of the form [namespace/](env-name)-v(version). (namespace is optional))

image

My solution -

In order to call your custom environment from a folder external to that where your custom gym was created, you need to modify the entry_point variable -

from gymnasium.envs.registration import register

register(
     id="gym_examples/GridWorld-v0",
     entry_point="path_to_environment_file:GridWorldEnv",
     max_episode_steps=300,
)

Could someone kindly confirm if this is the right approach? Many thanks!

Kallinteris-Andreas commented 11 months ago

You need to import your library, which contains your custom env

example:

import gymnasium
import my_library   # contains "my_environment"

env =  gymnasium.make("my_environment")
pseudo-rnd-thoughts commented 8 months ago

You will need to include in the my_library/__init__.py the registration code but the code above should work. Let us know if you still have issues