Multiple py files in actions

Hi, I want to connect mysql db to bot rasa.
Rasa version:

Rasa Version      :         2.8.1
Minimum Compatible Version: 2.8.0
Rasa SDK Version  :         2.8.1
Rasa X Version    :         0.39.3
Python Version    :         3.8.13

I created a db_connect.py file in “actions” folder in this way:

import mysql.connector
def DataFetch(luogo, preferenza, dispatcher):
    mydb = mysql.connector.connect(
        host="localhost", 
        user="root", 
        password="", 
        database="itourist"
    )

    mycursor = mydb.cursor() 
    sql = 'select Risultato from itourist.riscontro WHERE Luogo={"0"} && Preferenza={"1"} ordered by rand() limit 1;'.format(luogo, preferenza)

    try:
        #Execute the SQL Query
        mycursor.execute(sql) 
        result = db.query(sql)

        #Now print fetched data
        dispatcher.utter_message(result)

    except:
        dispatcher.utter_message("Error : Unable to fetch data.")

and I added this action in actions.py file :

from typing import Any, Text, Dict, List

from rasa_sdk import Action, Tracker
from rasa_sdk.executor import CollectingDispatcher
from db_connect import DataFetch

class ActionFetchData(Action):
    def name(self):
        return "action_match_events" # Be careful, did you mean action_fetch_data?
    
    def run(self, dispatcher, tracker, domain):
        DataFetch(tracker.get_slot("luogo"), tracker.get_slot("preferenza"), dispatcher)

        return []

But I have a problem: rasa train → works rasa run actions → works rasa shell → works but when the bot has to do that action , a message in the terminal appers :

File “C:\Users\luigi\Desktop\esempi_rasa\provautf\actions\actions.py”, line 14, in import db_connect ModuleNotFoundError: No module named ‘db_connect’

can you help me please? The purpose of the query is to show a casual result after matching 2 slots (rasa) with 2 fields of table in db.

@nik202 sorry for quoting you, but I remember you were very helpful with an other problem. Could you help me with this one?

Do you have an __init__.py file inside the actions folder? If not, please create it and try again.

I also suggest reading this article.

A post was split to a new topic: Action.py text not displaying

You merged both files? Did my solution not work?

As for your new problem, since it is unrelated, I moved it to a new topic: Action.py text not displaying

I didn’t try it because I found the solution before than your reply, but when I will solve this other problem I’ll try it for sure! Thanks for the new thread and sorry for the off topic!

1 Like

No worries for the off-topic post :slight_smile:

But if you ever come back to this problem please let me know if it worked, to help out future readers.

@luisshinken Chris is the right man :slight_smile: thanks Chris.

1 Like

In run module, write db_connect.DataFetch(). Also check whether the python files are in the same folder.