How to set the working directory when running on the cloud?

TL;DR:
Imports which locally work fine, don’t work on streamlit cloud


Hello, I have a problem with setting working directory for the project run on the community cloud. I have the following code structure and my working directory is src because I have two parts of my application, and one of them is streamlit.

── README.md
├── requirements.txt
├── src
│   ├── some_package
│   │   ├── main.py
│   │   ├── another_file.py
│   │   
│   ├── streamlit_dashboard
│   │   ├── functions
│   │   │   ├── file.py
│   │   │   ├── file1.py
│   │   │   ├── __init__.py
│   │   ├── __init__.py
│   │   ├── main_streamlit.py 
│   │   └── pages
│   │       ├── __init__.py
│   │       ├── page_1.py
│   │       ├── page_2.py
│   │       └── page_3.py

In my code I have a certain structure of imports, which start from the level just under src/, e.g.:

from some_package.another_file import foo


Locally, I run it from src/ folder level and set PYTHONPATH='.':

streamlit run streamlit_dashboard.main_streamlit.py

and everything works fine. However after uploading it to the cloud, to keep it working, I need to rearrange structure of imports. Just like the working directory was where main_streamlit.py is.

I tried to set PYTHONPATH to "/mount/src/my_project_name/src" in secrets, but it doesnt make it work.

PYTHONPATH doesn’t point to the working directory. To change the working directory in any python program running anywhere, use os.chdir()

Thank you for the response. That’s a good point however it was not enough. Eventually I needed also to append sys.path:

import os; os.chdir("/mount/src/<project-name>/src")
import sys; sys.path.append("/mount/src/<project-name>/src/")

Maybe relative paths also could work but I tried various combinations and it is the only one that works.

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