RASA action server failing ModuleNotFoundError: No module named 'myfunc'

Hi Team, I am very new to Python & RASA.

I am trying to run rasa run actions but receiving errors: ModuleNotFoundError: No module named ‘myfunc’

Background: I have created a new py file as myfunc.py with a simple program to return string. I have imported myfunc.py in actions.py but when I trigger the action server it fails with the error ModuleNotFoundError: No module named ‘myfunc’.

Actions.PY code:

from myfunc import *
from typing import Any, Text, Dict, List
#
from rasa_sdk import Action, Tracker
from rasa_sdk.executor import CollectingDispatcher


a = my_function()

class ActionHelloWorld(Action):

    def name(self) -> Text:
        return "action_hello_test"

    def run(self, dispatcher: CollectingDispatcher,
            tracker: Tracker,
            domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:

        dispatcher.utter_message(a)

        return []

Location:

Can someone help me to identify my mistake?

Regards, Mayank Pande

Welcome Mayank @mayank2288 it should be

from myfunc import <function name>

can you even add the myfunc.py file OR check this Python - Call function from another file - GeeksforGeeks

Welcome to the forum :slight_smile:

Try from . import myfunc or from .myfunc import my_function

Hi All, Thank you for response. It worked, but I changed myfunc.py path to the main folder. All code is the same.

Do we need to keep all py files under the root folder?

Regards, Mayank Pande

Which one worked? And do you have __init__.py in your actions folder?

@mayank2288 That’s quite strange whilst moving file outside from the actions folder which have action.py and __init__.py working fine with you. Normally all related files should be in the actions project folder in which the file is called.

1 Like

Hi Chris, Yes, I have __init__.py in the actions folder. It was created with the rasa init command.

Regards, Mayank Pande

Please answer this as well so we can help you

HI Chris, I moved myfunc.py file under main folder. i.e D:/Chat Bot. This is only change I did.

Regards, Mayank Pande

@mayank2288 Can you close this thread as solution for yourself and for others?

Hi Nik202.

Thank you . Yes, I can close the topic. But just to check, if I am doing things correctly? Is it the correct rasa way to call other py files from actions?

Regards. Mayank Pande

If you’re satisfied with the result then good enough.

But I suggest keeping all in the action folder using a method provided by Nik or me - whichever works :slight_smile:

@mayank2288 for me and honest answer is “No” as a developer we normally put all the files in the actions.py folder (calling functions) its safe and easy to find and maintained. But as Chris says if this give your result and satisfied your use case or project its good to go. Good Luck!

Ref: https://github.com/RasaHQ/rasa-demo/tree/main/actions | https://github.com/RasaHQ/financial-demo/tree/main/actions

1 Like