Imports not working for Multipage app

On streamlit cloud, my multipage app cannot import modules from the directory above. The error looks like:

File "/home/appuser/venv/lib/python3.9/site-packages/streamlit/scriptrunner/script_runner.py", line 557, in _run_script
    exec(code, module.__dict__)
File "app/home.py", line 5, in <module>
    import lenders.elastic as elastic

ModuleNotFoundError: No module named 'lenders'

This ran fine on my local machine, which is weird.

My repo structure looks like this:

lenders
└── elastic.py
app
├── home.py
└── pages
    ├── charts.py
    └── table.py

How should I handle the import?

2 Likes

Hello guys,
I have the exact same issue if try to run the app on the docker or locally. I am unable to import modules which lie outside of the streamlit app directory. But in pycharm if i setup the run configuration to include my source root it works the app is able to load and work. I have also tried adding the path into the config.toml in .streamlit folder under [server] config but still no luck

Any guidelines how to do for multi page app as the streamlit file doesn’t lie in the root directory. This is what my directory structure looks like. Please the streamlit_app is the directory for the app and I want to import functionlist as a module

Mcking, For now, I am manually adding that dir to the syspath as a workaround:

sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

You might have done the same.

FWIW, also seeing some flakiness in session_state with multipage UI so maybe it is just not ready for prime time yet?

1 Like

I have the same issue around here.

It would be nice if the st.set_page_config command could include a hide_page parameter so that our functional .py files could lie inside the same directory.

@lukasmasuch @StreamlitTeam

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