Hi @Zakariyan_Coding, welcome to the forum!
My best guess is that you’re ending up with a circular import, where A imports from B, and B imports from A. At least, that’s how I usually end up getting errors like that.
I have a couple of suggestions:
- I would highly recommend avoiding manipulating sys.path except when there’s REALLY no other option. In the case of a streamlit app like this, there’s really no need.
Let me give a simple example of how you can avoid this:
Here’s my setup:
.
├── pages
│ └── page1.py
├── streamlit_app.py
├── tool1
│ └── helper.py
└── tool2
└── helper.py
And in both streamlit_app.py and page1.py, I write out what sys.path
contains
What you’ll see on streamlit_app.py
after running streamlit run streamlit_app.py
is:
Your case will probably look slightly different, but it will probably be a bit different. There is some uv-specific stuff, but the important think to notice is:
- The second item is
""
, which represents the base directory, where streamlit_app.py
is located
- After that, it shows the packages that are installed in the virtual environment, etc.
So, when it looks for imports, it will start with streamlit_app.py, then look at the directory that file is located in, and then for installed packages.
Very critically, if you visit page1, you will see that the sys.path list is THE SAME – this means that whatever imports you do from streamlit_app.py (from tool1.helper import A
, for example) will work in the exact same way in all the pages, without manipulating the path at all – so even though page1.py is located in the pages/
folder, it can import things the exact same way that you can from streamlit_app.py
So, no need to do any sys.path manipulation, just import however you would from streamlit_app.py, and it should work just fine.
- Simplify down to the simplest possible example which still shows the import error
This is pretty generic, and sometimes is hard to do, but the pattern I like to use is:
- Save my work as-is
- Start deleting as much code as I possibly can (in this case, probably deleting most of the app code, and just leaving the imports, and then removing as many of those as I can, and even deleting most of the code and imports in in the helper python files) until I find the specific import that is actually causing the issue
- Dig into that import until you can track down why it’s problematic, or at least have a really simple example where you can show all the code on the forum and show people how to reproduce the issue.
I don’t know for sure if #1 will help, but it should simplify your setup a lot, and make the import situation simpler. That might solve the issue, or at least eliminate one potential source of the issue.