Below is how I imagined to organise the code of a mutlipage app.
In each pages, I will need to “import” from various code in the utils folder.
Relative importing in this way doesn’t seem to work.
I guess this is because the way StreamLit works for multipages.
Would you see how to import for example “birdsDictionary” in the data page?
Woudl you organize the files in another way?
import utils.birdsDictionary as bd
bd.multiply(3, 4)
or
from utils.birdsDictionary import multiply
multiply(3, 4)
I actually have similar structure with what you posted.
You have ./pages/data.py
I prefer to have ./modules/data.py
where:
The former is for frontend with widgets etc and the later is for backend, doing some calculation to be used for the frontend.
I included the init.py files.
I still get this message:
ImportError: attempted relative import with no known parent package
I think this is so because the way StreamLit work.
If I go to the ‘listen’ page, the listen.py code is executed just like an new app.
With the help of StreamLit, this new app just receives the st.session_state.
I guess StreamLit does that I bit like passing argument, I don’t know exactly how.
But the end result is that this “app” listen.py apparrently does not have have access to code to the “parent package” as -my guess- there is not such parent in this context.
Any idea?
Thanks for you suggestions.
Michel
PS:
Everytime I come back to StreamLit, I fall on such difficulties…
Maybe “simple” is difficult for me !
Like for example adjusting the structure of code to fit the “pages” trick of st.
Based on your structure, you should be able to use absolute imports if your project’s root directory is correctly recognized as such. If birdsAudio is your project root, and you run your Streamlit app from there, you can import modules like so:
# In your pages/data.py
from utils.birdsDictionary import multiply
# Now you can use the function multiply() as needed
result = multiply(3, 4)
When running your Streamlit app, ensure the current working directory is the root of your project (birdsAudio ).
import os
print("Current Working Directory:", os.getcwd())
# If not correct, change it
os.chdir('/path/to/birdsAudio')
Hope this helps!
Kind Regards,
Sahir Maharaj
Data Scientist | AI Engineer
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.