[Solved] Problem in custom slot [ValueError: Failed to find slot type]

I created my own custom slot module slots.py (362 Bytes) with class ViewSlot. It is in the same folder as the domain.yml (3.6 KB). Inside domain.yml, I have a slot view_slot of type slots.ViewSlot, but still I’m getting the following error while training:

Traceback (most recent call last):
  File "c:\users\geuser\appdata\local\programs\python\python36\lib\site-packages\rasa\core\slots.py", line 68, in resolve_by_type
    return class_from_module_path(type_name)
  File "c:\users\geuser\appdata\local\programs\python\python36\lib\site-packages\rasa\utils\common.py", line 188, in class_from_module_path
    m = importlib.import_module(module_name)
  File "c:\users\geuser\appdata\local\programs\python\python36\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 953, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'slots'

    During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "c:\users\geuser\appdata\local\programs\python\python36\lib\runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "c:\users\geuser\appdata\local\programs\python\python36\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "C:\Users\geuser\AppData\Local\Programs\Python\Python36\Scripts\rasa.exe\__main__.py", line 9, in <module>
  File "c:\users\geuser\appdata\local\programs\python\python36\lib\site-packages\rasa\__main__.py", line 70, in main
    cmdline_arguments.func(cmdline_arguments)
  File "c:\users\geuser\appdata\local\programs\python\python36\lib\site-packages\rasa\cli\train.py", line 84, in train
    kwargs=extract_additional_arguments(args),
  File "c:\users\geuser\appdata\local\programs\python\python36\lib\site-packages\rasa\train.py", line 40, in train
    kwargs=kwargs,
  File "c:\users\geuser\appdata\local\programs\python\python36\lib\asyncio\base_events.py", line 484, in run_until_complete
    return future.result()
  File "c:\users\geuser\appdata\local\programs\python\python36\lib\site-packages\rasa\train.py", line 72, in train_async
    domain = Domain.load(domain, skill_imports)
  File "c:\users\geuser\appdata\local\programs\python\python36\lib\site-packages\rasa\core\domain.py", line 73, in load
    other = cls.from_path(path, skill_imports)
  File "c:\users\geuser\appdata\local\programs\python\python36\lib\site-packages\rasa\core\domain.py", line 87, in from_path
    domain = cls.from_file(path)
  File "c:\users\geuser\appdata\local\programs\python\python36\lib\site-packages\rasa\core\domain.py", line 100, in from_file
    return cls.from_yaml(rasa.utils.io.read_file(path))
  File "c:\users\geuser\appdata\local\programs\python\python36\lib\site-packages\rasa\core\domain.py", line 110, in from_yaml
    return cls.from_dict(data)
  File "c:\users\geuser\appdata\local\programs\python\python36\lib\site-packages\rasa\core\domain.py", line 115, in from_dict
    slots = cls.collect_slots(data.get("slots", {}))
  File "c:\users\geuser\appdata\local\programs\python\python36\lib\site-packages\rasa\core\domain.py", line 205, in collect_slots
    slot_class = Slot.resolve_by_type(slot_dict[slot_name].get("type"))
  File "c:\users\geuser\appdata\local\programs\python\python36\lib\site-packages\rasa\core\slots.py", line 73, in resolve_by_type
    "sure its module path is correct.".format(type_name)
ValueError: Failed to find slot type, 'slots.ViewSlot' is neither a known type nor user-defined. If you are creating your own slot type, make sure its module path is correct.

I couldn’t figure out how to work with custom slot? The doc does not explain this very well. A detailed example will be much appreciated. Thanks!

P.S.: I used the command rasa train for training, do I need to specify anything else in this command?

python cannot import your custom class, you need to put the path to the class in your domain file

Ya so as per this thread, I defined my slot’s type as slots.ViewSlot in the domain file. Is there some other way of putting the path to the class in domain.yml? (my domain file and custom class file are in the same folder.)

hm, can you import the class from python shell?

Yes. from slots import ViewSlot is not giving any errors.

Okay so, I don’t know why, but setting the PYTHONPATH environment variable to my directory’s path worked. This is how you set it in windows. This for linux.

For me its different, if i import something other than a file from rasa, its not found anymore. Does your file import some other python file of your project or external lib too?