I have just finished working on a Flask web application that is using openai SDK.
Locally everything works smoothly, but after uploading the project on PythonAnywhere, the environment variables from .env file are not accessible.
This is the code I use to define OpenAI client:
from dotenv import load_dotenv
from openai import OpenAI
load_dotenv()
class Grader:
def __init__(self):
self.client = OpenAI(
api_key=os.getenv("OPENAI_API_KEY")
)
When the exact same code was running on PythonAnywhere, I kept getting this error:
openai.OpenAIError: The api_key client option must be set either by passing api_key to the client or by setting the OPENAI_API_KEY environment variable
The moment I replaced os.getenv("OPENAI_API_KEY") with the actual API key in plain string, everything started working as expected.
How to properly load the environment variables in PythonAnywhere? Do I need to do some configuration in the WSGI file?