Module Not Found -- Importing My Own Modules

I am running my multi-page streamlit app locally right now, and I have tried multiple approaches to solving this issue (I see others have the same issue) which is I need to import classes I created into a pages/_1_page.py. So I have tried three approaches:

APPROACH 1:
Use the following folder structure:

|__frontend
            |___main.py
            |___util_1.py
            |___pages
                        |__page.py

In page.py I want to import MyClass, a class found in util_1.py. I have tried:

from src.frontend.pages.util_1.py import *

APPROACH 2:
I tried changing the file structure so that util_1.py is in a subdirectory underneath pages like this:

|__frontend
            |___main.py
            |___pages
                        |__page.py
                        |__subdirectory
                                           |___util_1.py

Then I had page.py like this:

from src.frontend.pages.subdirectory import util_1

I have also tried (as some have suggested):

from pages.subdirectory import util_1

Neither worked. I don’t want to just put util_1 under the pages folder or else it will automatically show up as a page which I don’t want.

APPROACH 3:
I have tried to solve this programmatically using either:

sys.path.append(str(Path(__file__).resolve().parent.parent))

OR

sys.path.append(os.path.dirname(os.path.abspath(__file__))+"../")

Neither worked.

QUESTION:
There has GOT to be a way to import my own code (not just third party libraries) into a page, no?

I saw on one chat that it could be a requirements.txt issue, but that should only be for third party libraries, correct? Not python modules I create in my own file structure, right?

1 Like

Hi @Rasputin

I have a multi-page app (GitHub - dataprofessor/st-multipage) that has my own custom functions in utilities.py file which is in the same folder as the app Home.py file.

I’d import my custom functions from utilities.py file like so:

from utilities import function_1

Hope this helps!

2 Likes

Unfortunately, that was the first thing i tried, but my ide (pycharm) doesnt even recognize it. It will recognize it if I say (when i am in src.frontend.pages.page1):

from src.frontend.utilities import xyz

But then when i run streamlit it says it doesnt recognize the src folder. Basically, i think the issue is that if i run streamlit from within the src folder it treats src as root. Thats fine, but then i cannot seem to import classes or functions from other files if i am in the pages.