File not found error for SQL script file on Streamlit Cloud - Working fine in my local environment


My app is reading data from a BigQuery database. The SQL script is in a separate .sql file and is being read from the .py file. It works fine when I run the app locally, but I am getting a file not found error after deploying the app on Streamlit Cloud. Here is the error message -

Traceback (most recent call last):

  File "/home/appuser/venv/lib/python3.10/site-packages/streamlit/scriptrunner/", line 557, in _run_script

    exec(code, module.__dict__)

  File "bq-streamlit-poc/pages/", line 34, in <module>

    sql_tw_global = open("pages/tw-global.sql", "r")

FileNotFoundError: [Errno 2] No such file or directory: 'pages/tw-global.sql'

Both the .py and .sql files are in the pages folder.
Any help would be appreciated. Thanks.

I found the solution. We need to set the path using __file__. It should be like this -

os.path.join(os.path.dirname(__file__), "file_name")

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