DLAI Course - trouble loading OpenAI API key

to better understand this, you could try inserting a test for the presence of the environment variable like this…

from dotenv import load_dotenv, find_dotenv
_ = load_dotenv(find_dotenv())

print('OPENAI_API_KEY' in os.environ)

if it outputs False then you know OPENAI_API_KEY hasn’t been set in the environment and you can’t use the os.getenv() method to retrieve it. Instead, use one of the steps provided in a reply above.

or do this…

os.environ['OPENAI_API_KEY'] = 'sk-my-openai-api-key-string-here'
print('OPENAI_API_KEY' in os.environ)

openai.api_key  = os.getenv('OPENAI_API_KEY')