'How to solve OpenAI gym's 'Module has no attribute error'

I follow the exact same folder structure for OpenAI Gym but still get attribute not found error upon using gym.make(). My folder structure is as below.

my_gym
  -->examples
      ---MAIN_ENV 
  -->my_rl_gym
        --->envs
               ---__init__
               ---main_env    #contains a class called Myxyz as registered below
        ---__init__

  -->setup.ipynb

The below is how I register in the __init__ inside envs folder

from gym.envs.registration import register

register(
     id='XYZ-v0',
     entry_point='my_rl_gym.envs:Myxyz'
)

The below is in the __init__ inside my_rl_gym folder i.e. outside envs

# from my_rl_gym.envs.main_env import Myxyz

#### THIS above line is actually correct BUT gives error as No module named 
#####'my_rl_gym.envs.main_env ' . Hence, I changed this to below line.

import main_env 

The error comes upon calling the make command

env = gym.make('XYZ-v0', **env_args)

Traceback is:

----> 6 env = gym.make('XYZ-v0', **env_args)
      
~\anaconda3\lib\site-packages\gym\envs\registration.py in make(id, **kwargs)
    674 # fmt: on
    675 def make(id: str, **kwargs) -> "Env":
--> 676     return registry.make(id, **kwargs)
    677 
    678 

~\anaconda3\lib\site-packages\gym\envs\registration.py in make(self, path, **kwargs)
    518         spec = self.spec(path)
    519         # Construct the environment
--> 520         return spec.make(**kwargs)
    521 
    522     def all(self):

~\anaconda3\lib\site-packages\gym\envs\registration.py in make(self, **kwargs)
    137             env = self.entry_point(**_kwargs)
    138         else:
--> 139             cls = load(self.entry_point)
    140             env = cls(**_kwargs)
    141 

~\anaconda3\lib\site-packages\gym\envs\registration.py in load(name)
     54     mod_name, attr_name = name.split(":")
     55     mod = importlib.import_module(mod_name)
---> 56     fn = getattr(mod, attr_name)
     57     return fn
     58 

AttributeError: module 'my_rl_gym.envs' has no attribute 'Myxyz'

What is the problem here? The folder directories are correct and the way of defining is also correct. For e.g. see https://github.com/MartinThoma/banana-gym Is it because I need to have any setup file? because if so then I did and got the typeerror as in question Getting Type error with setup file in OpenAI gym



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source