Import Error during first run only - Refreshing will clear the import error

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

1 Like

Looks like similarity_result is not defined in module backend.utils.similarity.

It certainly is. My IDE picks it up, no problem. I’ve triple-checked. And it works on refreshes.

First starting up the project is the problem. Another confusing factor - sometimes (seldomly), when I load Home.py, I’ll see Analysis.py already running in the background (confirmed in terminal via logger, etc.), then when I go to Analysis.py, there will be no import issue. This variability makes it all the more strange.

1 Like

I set up a source tree following your description and it works without issues for me.

@Goyo
I’m also facing this same issue. My scenario is where I have a Login component using stAuth itself and based on the authentication_status returned I will render the main page content, which includes Imports as well. And on the first successful authentication when it imports my Services folder, it shows
ImportError: cannot import name ‘CustomerRecordService’ from ‘Services’ (unknown location),
But re-running the same page - it works fine!

Can we have any dev look at this?

Versions
Python 3.8.8
Streamlit, version 1.25.0

Regards

Look at what? There is not much to look at in your post.

Please read the guidelines on how to post an effective question in the forum and check out the 10 most common answers to forum questions.

I can confirm I have a very similar error. A basic Python code structure, where on the first run a class fails to import, but when the Streamlit app is refreshed it imports just fine. The error is consistent.

The code structure is as follows:

app/init.py - misc. setup code

app/auth.py

...
class Login:
   ...
...

app/main.py

...
from app.auth import Login
...

I’ll try see if I can reproduce it with a minimum viable example and share.

I created a minimum viable example and fixed the error on my side, but still not 100% sure of the root cause.

I have a package called streamlit_cookies_manager in app/streamlit_cookies_manager/. Within that package, there’s a module called encrypted_cookie_manager.py. This module was importing streamlit_cookies_manager as if it were installed and in the Python path, which it isn’t - I had to include the code manually because the maintainer of the package hasn’t published the code.

So, the fix was changing:

# Broken import
from streamlit_cookies_manager import CookieManager

# Fixed import
from app.streamlit_cookies_manager import CookieManager

But, maybe still unusual that the error appeared as an import error of the Login class (which uses the CookieManager), and that refreshing the Streamlit app then succeeds (maybe at that point streamlit_cookies_manager has been added to the Python path).

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.