Search code examples
pythonjupyter-notebookgoogle-colaboratoryopenai-gym

Register gym environment that is defined inside a jupyter notebook cell


I'm trying to register an environment that has been defined inside a cell of a jupyter notebook running on colab. My problem is concerned with the entry_point. Some module has to be specified before the colon. But how do I know the module's name of the underlying colab notebook? Is there some workaround to wrap my custom environment inside a module without moving the code to another file? I'd prefer to have everything inside the entire notebook.

try:
  gym.envs.register(
      id='myenv-v0',
      entry_point=':MyEnv',
      max_episode_steps=320
  )
except:
    pass

If I call gym.make("myenv-v0")

ModuleNotFoundError: No module named 'base'

The try block is due to the fact that an evnrinoment id cannot be registered twice. It may happen that I run again all notebook cells.


Solution

  • After writing this question I finally figured out the right key words to conduct a proper search to find the solution.

    The underlying module name can be retrieved using

    import sys
    sys.modules[__name__]
    

    Therefore the module's name for the entrypoint is "main".

    try:
      gym.envs.register(
          id='myenv-v0',
          entry_point='__main__:MyEnv',
          max_episode_steps=320
      )
    except:
        pass