Open AI error Key not found

Hi,
I am new to openai and trying to run the example code to run a bot.
import os
import openai

openai.api_key = os.getenv(“APIKEY”)

response = openai.Completion.create(
engine=“text-davinci-001”,
prompt=“Marv is a chatbot that reluctantly answers questions with sarcastic responses:\n\nYou: How many pounds are in a kilogram?\nMarv: This again? There are 2.2 pounds in a kilogram. Please make a note of this.\nYou: What does HTML stand for?\nMarv: Was Google too busy? Hypertext Markup Language. The T is for try to ask better questions in the future.\nYou: When did the first airplane fly?\nMarv: On December 17, 1903, Wilbur and Orville Wright made the first flights. I wish they’d come and take me away.\nYou: What is the meaning of life?\nMarv: I’m not sure. I’ll ask my friend Google.\nYou: What time is it?\nMarv:”,
temperature=0.5,
max_tokens=60,
top_p=0.3,
frequency_penalty=0.5,
presence_penalty=0.0
)
This code is taken from one of the examples: I have given the key values. But still getting error
as " You can set your API key in code using 'openai.api_key = ', or you can set the environment variable OPENAI_API_KEY=). If your API key is stored in a file, you can point the openai module at it with 'openai.api_key_path = '. You can generate API keys in the OpenAI web interface. See https://onboard.openai.com for details, or email support@openai.com if you have any questions."

Any help?

1 Like

You need to check to see if the environment variable named ‘APIKEY’ actually exists. When I originally installed the Python module, I believe the environment variable was named ‘OPENAI_API_KEY’, not ‘APIKEY’ like you have it in the os.getenv() function.

If changing that string to ‘OPENAI_API_KEY’ doesn’t work either, then do this on your machine:

  1. Launch “Control Panel”
  2. “System”
  3. “Advanced system settings”
  4. Switch to “Advanced” tab
  5. “Environment variables”
  6. Choose “User Variable” (for just you since any other accounts on your system could access System Variables)
  7. To add a new environment variable: Choose “New”
  8. Enter the variable name “OPENAI_API_KEY” and the actual API string then save it.

You need to RE-START the Python interactive session if you have it running on your system for the new setting to take effect.

Hope this helps!

maybe try this …

import os
import openai

openai.api_key = ('sk-gjtvKEn544fJxxxx)

I removed the “os.getenv”

openai.api_key = os.getenv(“APIKEY”)

Screenshot from 2022-02-23 03-13-56

1 Like

@oieieio,

I thought you couldn’t initialize a variable in Python like that, but I wrong! Learn something new every day.

Anyways, it’s best practice not to hard-code your API key into your script like that and simply reference environment variables to reduce the likelihood of exposing sensitive information to others and to increase the security of your code.

Side-Note: I know that your whole API key isn’t visible, but I’d still take down the screenshot if I were you. The API key in the screenshot is now much easier to bruteforce given that most of the key is exposed. That is unless you already regenerated another API key.

1 Like

I’m pretty new at this so thanks for the best practice tip about the key…

I don’t mind the screenshot I already revoked that key.

I’m learning

1 Like

Practice makes perfect!

This line in the script should work once you create the ‘OPENAI_API_KEY’ environment variable first:
openai.api_key = os.getenv("OPENAI_API_KEY")

2 Likes

Thank you. Adding the environment variable generated the following errors after import openai
Traceback (most recent call last):
File “”, line 1, in
File “C:\Users\B\AppData\Local\Programs\Python\Python310\lib\site-packages\openai_init_.py”, line 8, in
from openai.api_resources import (
File “C:\Users\B\AppData\Local\Programs\Python\Python310\lib\site-packages\openai\api_resources_init_.py”, line 1, in
from openai.api_resources.answer import Answer # noqa: F401
File “C:\Users\B\AppData\Local\Programs\Python\Python310\lib\site-packages\openai\api_resources\answer.py”, line 1, in
from openai.openai_object import OpenAIObject
File “C:\Users\B\AppData\Local\Programs\Python\Python310\lib\site-packages\openai\openai_object.py”, line 6, in
from openai import api_requestor, util
File “C:\Users\B\AppData\Local\Programs\Python\Python310\lib\site-packages\openai\api_requestor.py”, line 13, in
from openai import error, util, version
File “C:\Users\B\AppData\Local\Programs\Python\Python310\lib\site-packages\openai\util.py”, line 6, in
response = openai.Completion.create(
AttributeError: partially initialized module ‘openai’ has no attribute ‘Completion’ (most likely due to a circular import)

Thank you. Removing os.getenv runs on Python command prompt but does nothing. Getting errors in pycharm " AttributeError: partially initialized module ‘openai’ has no attribute ‘Completion’ (most likely due to a circular import)"