Multipage changes __name__ to __page__ inside a page

When using a multipage app and rendering a page using st.navigation, the __name__ value is set to '__page__'. This means that I cannot use relative imports while inside a .py file rendered using st.navigation.

Use case

I have a folder for all pages called pages_folder. Then, for each page, I have a subfolder with a file I use to display info and another which I use to run some logic. For example, a login page has a display.py which renders the form, and a component.py file that defines a function login(user, passw) called when the form in display.py is submitted.

I would like to import login inside display.py using a relative import, since these files will always be together.

from .component import login

This raises ImportError: attempted relative import with no known parent package, since display.py is rendered using st.navigation. removing it from any modules. Therefore, I have to use absolute imports, making it harder to move this folder around if I need to.

How to reproduce

Happens on any multipage app. I’ve cloned the hello project from streamlit and made some changes in order to reproduce this behavior.

The app prints __name__ everytime the page changes, at the start of the page and at the hello.py file before calling pg.run(). There’s a print __name__ button defined at hello.py that calls one function from each page file, printing __name__. Whenever streamlit reruns and prints __name__ normally, it shows that __name__ is __page__ for both pages, but when it prints __name__ due to pressing the print button, it shows the right __name__ and pages module.

1 Like

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