Error while loading API key

I am trying to load the OpenAI API_KEY into my main.py 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/script_runner.py", line 565, in _run_script** ** exec(code, module.__dict__)** **File "/app/cannabis-recommendation-using-ml/main.py", line 44, in <module>** ** openai.api_key=os.environ['API_KEY']** **File "/usr/local/lib/python3.10/os.py", 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 main.py.




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

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