Error while loading API key

I am trying to load the OpenAI API_KEY into my script (streamlit application) and while everything runs smoothly when run from local, but as soon as I host my app on streamlit cloud, I get an error
**KeyError: This app has encountered an error. The original error message is redacted to prevent data leaks. Full error details have been recorded in the logs (if you're on Streamlit Cloud, click on 'Manage app' in the lower right of your app).** **Traceback:** **File "/home/appuser/venv/lib/python3.10/site-packages/streamlit/runtime/scriptrunner/", line 565, in _run_script** ** exec(code, module.__dict__)** **File "/app/cannabis-recommendation-using-ml/", line 44, in <module>** ** openai.api_key=os.environ['API_KEY']** **File "/usr/local/lib/python3.10/", line 680, in __getitem__** ** raise KeyError(key) from None**

I am storing my OpenAI API_KEY in a .env file which has already been pushed to the gitlab(but couldnt be discovered due to .gitignore). Here is my snapshot of my .env file , .gitignore file and the

It can’t work that way either and that’s the wrong approach. How should Streamlit Cloud get hold of the .env file if its not in the repo??? Here is the correct approach:

1 Like