Summary
When I first run streamlit run Home.py
, then try to navigate to another page, I receive this import error:
2023-05-18 09:34:22.531 Uncaught app exception
Traceback (most recent call last):
File “/Users/s/projects/chat/venv/lib/python3.11/site-packages/streamlit/runtime/scriptrunner/script_runner.py”, line 565, in _run_script
exec(code, module.dict)
File “/Users/s/projects/chat/frontend/pages/1_Analysis.py”, line 76, in
from backend.utils.similarity import similarity_result
ImportError: cannot import name ‘similarity_result’ from ‘backend.utils.similarity’ (/Users/s/projects/chat/backend/utils/similarity.py)
When I refresh the page, the error disappears and it works as expected.
Results of print(sys.path)
from within similarity.py:
['Home.py', '', '/Users/s/projects/chat/venv/lib/python3.11/site-packages/git/ext/gitdb', '/Users/s/projects/chat/venv/bin', '/Library/Frameworks/Python.framework/Versions/3.11/lib/python311.zip', '/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11', '/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/lib-dynload', '/Users/s/projects/chat/venv/lib/python3.11/site-packages', '/Users/s/projects/chat', '..', '/Users/s/projects/chat', '/Users/s/projects/chat']
This is what the folder structure looks like:
chat/
├── init.py
├── frontend/
│ ├── init.py
│ ├── Home.py
│ └── pages/
│ ├── init.py
│ └── 1_Analysis.py
└── backend/
├── init.py
├── utils/
│ ├── init.py
│ └── similarity.py
Here’s how I’m setting the path in Home.py:
current_dir = os.path.dirname(os.path.abspath(__file__))
parent_dir = os.path.dirname(current_dir)
# grandparent_dir = os.path.dirname(parent_dir)
sys.path.insert(0, parent_dir)
Basically, from Analysis.py, I’m trying to get a function in the parent’s parent’s sibling’s child’s child. From similarity.py to Analysis.py
I know that it’s going into the file, since I’m able to print the path from within the file, also because it does work and import the file upon refresh. Not sure why the first paint is returning the error.
- Streamlit version: version 1.22.0
- Python version: Python 3.11.2
EDIT: (more info & interesting observation)
Here’s what the continous output looks like. It starts from streamlit run Home.py
until the exception. Then I refresh the page and it works as usual:
2023-05-18 13:57:32.586 | DEBUG | __main__:<module>:76 - FROM _ANALYSIS: ['Home.py', '/Users/s/projects/chat', '', '/Users/s/projects/chat/venv/lib/python3.11/site-packages/git/ext/gitdb', '/Users/s/projects/chat/venv/bin', '/Library/Frameworks/Python.framework/Versions/3.11/lib/python311.zip', '/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11', '/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/lib-dynload', '/Users/s/projects/chat/venv/lib/python3.11/site-packages']
2023-05-18 13:57:33.510 | DEBUG | __main__:<module>:76 - FROM _ANALYSIS: ['Home.py', '/Users/s/projects/chat', '', '/Users/s/projects/chat/venv/lib/python3.11/site-packages/git/ext/gitdb', '/Users/s/projects/chat/venv/bin', '/Library/Frameworks/Python.framework/Versions/3.11/lib/python311.zip', '/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11', '/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/lib-dynload', '/Users/s/projects/chat/venv/lib/python3.11/site-packages']
2023-05-18 13:57:36.141 | DEBUG | backend.utils.similarity:<module>:18 - FROM SIMILARITY ['Home.py', '/Users/s/projects/chat', '', '/Users/s/projects/chat/venv/lib/python3.11/site-packages/git/ext/gitdb', '/Users/s/projects/chat/venv/bin', '/Library/Frameworks/Python.framework/Versions/3.11/lib/python311.zip', '/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11', '/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/lib-dynload', '/Users/s/projects/chat/venv/lib/python3.11/site-packages']
2023-05-18 13:57:36.174 Uncaught app exception
Traceback (most recent call last):
File "/Users/s/projects/chat/venv/lib/python3.11/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 565, in _run_script
exec(code, module.__dict__)
File "/Users/s/projects/chat/frontend/pages/1__Analysis.py", line 77, in <module>
from backend.utils.similarity import similarity_result
ImportError: cannot import name 'similarity_result' from 'backend.utils.similarity' (/Users/s/projects/chat/backend/utils/similarity.py)
<REFRESHED THE PAGE HERE>
2023-05-18 13:58:04.455 | DEBUG | __main__:<module>:76 - FROM _ANALYSIS: ['Home.py', '/Users/s/projects/chat', '', '/Users/s/projects/chat/venv/lib/python3.11/site-packages/git/ext/gitdb', '/Users/s/projects/chat/venv/bin', '/Library/Frameworks/Python.framework/Versions/3.11/lib/python311.zip', '/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11', '/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/lib-dynload', '/Users/s/projects/chat/venv/lib/python3.11/site-packages']
2023-05-18 13:58:04.474 | DEBUG | backend.utils.similarity:<module>:18 - FROM SIMILARITY ['Home.py', '/Users/s/projects/chat', '', '/Users/s/projects/chat/venv/lib/python3.11/site-packages/git/ext/gitdb', '/Users/s/projects/chat/venv/bin', '/Library/Frameworks/Python.framework/Versions/3.11/lib/python311.zip', '/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11', '/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/lib-dynload', '/Users/s/projects/chat/venv/lib/python3.11/site-packages']
2023-05-18 13:58:05.169 | DEBUG | backend.utils.similarity:<module>:88 - ALL THE STORES: [('February_2023', <langchain.vectorstores.faiss.FAISS object at 0x126464790>), ('March_2023', <langchain.vectorstores.faiss.FAISS object at 0x127704110>)]
<WORKING AS USUAL FROM HERE>
Via the logger, I can see whether a file is running as main. Interesting that main is the page of Analysis.py - I wonder if this has something to do with the issue? Didn’t know streamlit will cast any streamlit “page” that I’m using as the “main”.
P.S: what’s being printed from the “FROM _ANALYSIS…” etc is just sys.path