How do I get import myFile to work? (importing into actions.py)

I have a separate python script, saved within the same directory as the actions.py, that I made. I want to import that file into my actions.py file so that I can expand upon the features while keeping an orderly/well organized system. I have had zero issues importing other libraries; However, all other libraries are from 3rd parties. I am wondering if you cannot import your own files? Anyway, please let me know if I am stupid or this is just not possible. I am just not familiar with python enough I guess.

1 Like

You can definitely import your own file! It sound like what you’re looking for is called a relative import. So for example:

from . import myFile

or:

from .myFile import my_function

2 Likes

Thank you so much @fkoerner! Wow, that’s embarrassing. I am brand new to python, so hopefully my issue is excusable for beginners. :face_with_raised_eyebrow:

2 Likes

You’re welcome! No need to be embarrassed: everyone starts as a beginner :slight_smile:

Happy bot-building!

2 Likes

Hello @fkoerner! Sorry, I know that’s not Rasa related but…

I’ve written helper functions and global variables in actions.py.

If I want to split my actions file into let’s say, an action file for a particular form, an action file for this kind of responses, etc.

So, in the main actions.py file, I can import the other files. But if a custom action needs to access a helper function defined in actions.py, do I need to import the files in each other? For example:

  • actions.py: from . import other_actions
  • other_actions.py: from . import actions

@ChrisRahme, you can import all your own files into your actions.py file. I have also found another thread looked as though someone got their rasa to run multiple actions.py files. Here is that thread. What I have done was import my additional actions files directly into my actions.py file. The files that were mine are the “EmailFunctions” and the “LoadData” (as seen in the image below).

Untitled

1 Like

Thanks for your answer. I created a common.py file with global variables that I’m importing in each file. Haven’t tested it yet though.

I also found this which helped.

Hi @ChrisRahme, looks like you already got the help you needed! I’ll add that what you described above:

  • actions.py: from . import other_actions
  • other_actions.py: from . import actions

is a cyclic import (module A imports module B, and module B imports module A). This isn’t always a problem, but it can become one. Little anecdote here.

What you describe later is a better pattern – put the methods and constants you want to import into both action files into a common.py.

1 Like

Thanks!

1 Like