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

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.

├── requirements.txt
├── src
│   ├── some_package
│   │   ├──
│   │   ├──
│   │   
│   ├── streamlit_dashboard
│   │   ├── functions
│   │   │   ├──
│   │   │   ├──
│   │   │   ├──
│   │   ├──
│   │   ├── 
│   │   └── pages
│   │       ├──
│   │       ├──
│   │       ├──
│   │       └──

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

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 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.