Reputation: 706
I am trying to build a multipage dashboard where each page uses functions written in a separate .py files. Title_page.py
can read function (called 'add_two()') from func_1.py
without any problem. However, Page2.py
can't seem to read function (called 'multiply_two()') from func_2.py
under p2_functions folder
, which is placed inside the pages
folder. It throws an error saying:
ModuleNotFoundError: No module named 'p2_functions'
I think it's because streamlit ignore anything other than .py files inside the pages
folder, but I don't know how then I could call modules and read functions from other .py files or from other folders.
Here's what I have in the Page2.py:
import streamlit as st
from p2_functions.func_2 import multiply_two
st.markdown("# Page 2 ")
st.write(multiply_two(10 * 20))
Here's the tree of the structure:
├── Title_page.py
├── func_1.py
└── pages
├── Page2.py
└── p2_functions
└── func_2.py
Upvotes: 1
Views: 5252
Reputation: 5721
You need to insert the p2_functions
folder path into Page2.py
if they are from different folders.
E,g
import sys
# Insert functions path into working dir if they are not in the same working dir
sys.path.insert(1, "C:/<p2_functions folder path>/") # Edit <p2_functions folder path> and put path
from func_2 import multiply_two
That is the way streamlit can recognise other files from other paths
Upvotes: 1
Reputation: 706
from pages.p2_functions.func_2 import multiply_two
Imports in Streamlit always work relative to the directory where you run streamlit run ..., so in your case the root of your directory.
Upvotes: 1