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?

3 Likes

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.

Did you try a local import?

from .utilities import xyz

Or within PyCharm try right clicking on your src folder and setting Mark Directory as > Sources Root so that pycharm will be happy with you doing:

from frontend.utilities import xyz

The answer turned out to be that the filepath in local and in Docker are different. So, basically, if in Docker you run the CMD streamlit run frontend/main.py then streamlit basically views the /frontend folder as your source directory and all imports have to be relative to that frontend folder. That is not the case when you are just on your PC apparently. So… when I do my imports in my local, I have to just ignore the errors Python gives me, and set the imports relative to the frontend folder. If I were to run streamlit on my PC (not inside Docker) then, I’d have to use the src folder in PyCharm. It’s annoying and I could probably figure out some way to automate switching the file prefix, but I just resigned myself to always running in Docker.

I managed to make it work with
sys.path.append(str(pathlib.Path(__file__).parent.parent))
to import from frontend, with the streamlit script in the same frontend folder