Search code examples
pythonpathpycharmstreamlit

How to Deal with Differing Roots in Local and Remote


I am using PyCharm and I have a file structure like this in a Streamlit app:

myproject
|-.env
|-deployment_folder
|-src
|-|-.streamlit
|-|-frontend
|-|-|-data.txt
|-|-|-main.py

Inside main.py, if I want to open data.txt I either have to use the absolute path to data.txt (i.e., /Users/me/PycharmProjects/myproject/src/frontend/data.txt) or a relative path from the frontend directory ("./data.txt").

Unfortunately for whatever reason Streamlit seems to have difficulty with relative addressing, so I have to use absolute path when I run it locally. The problem is that this is all within a Docker container and when I push to a remote environment, the "root" is set as the src folder. So this means everytime I want to run the streamlit app locally to debug I have to keep switching the filepaths, which is driving me nuts. I have to use absolute when I am local and then absolute starting from src as root before I push.

I tried in PyCharm editing my configurations to set src as root but that didn't seem to change anything. Stated differently, the 'root' in my local is different from the 'root' when I push and this is run in a Docker container in the kubernetes cluster.

Any suggestions?


Solution

  • You can access the current path of main.py inside main.py like this:

    # in main.py
    import pathlib
    
    current_path = pathlib.Path(__file__)
    

    So, wherever you deploy your application, this would give you the absolute path of the current file running. Finding data.txt is just:

    # in main.py
    import pathlib
    
    current_path = pathlib.Path(__file__)
    data_txt_path = (current_path.parent / "data.txt").resolve()