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

I am trying to troubleshoot the following error.

johnsiano@Johns-MacBook-Air Desktop % python3 openai-test.py

Traceback (most recent call last):
File “/Users/johnsiano/Desktop/openai-test.py”, line 2, in
client = OpenAI()
File “/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/openai/_client.py”, line 101, in init
raise OpenAIError(
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

Solved, I reloaded my profile using the source command and then verified the setup by running echo, and it worked properly.

But this works only for the current session, if you close the session/project, and open it later, it gives the same error. Haven’t you found a permanent solution to this? This may also cause problems in production mode.