I’ve created a multi-page app. Obviously different pages will be mostly different, but I do want to apply same page formatting (company logo, copyright message, etc.).
I created a separate module with these formatting, and tried to import in the page scripts under pages directory, and the main script. It failed for scripts under pages, main one works.
When I place it outside pages, I can not import it as it complains about relative import without parent package.
As soon as I make pages as a package with a __init__ script, that gets treated as a lage which is not what I want.
When I place it inside pages, it gets treated as a page automatically. So once again, this doesn’t help me.
So, I want to know how to import custom codes (modules or packages, but not packages that are installed) in the scripts under pages. Also, it’d be good to know if there’s a way to tell streamlit to ignore a script under pages as an app page.
I also came up against this when turning a single script into a multipage app. The solution for me was to modify the PYTHONPATH to include the top-level directory before doing the imports within the *.py files within pages. Doesn’t look particularly nice, but it is what it is given the limitations of relative imports.
Very interesting approach. As you said, it does look bad, but it’s solves the problem. I changed it to insert at 0th position instead of appending it at end, but that’s just my own preference I guess, it works exactly as you shared as well.
But I hope there’ll be a native streamlit solution soon.
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.