"No module named 'openai'"

151 Views Asked by At

My code won't run due to an error message in my terminal stating: "No module named 'openai'". It wasn't personally written by me, but taken from a .py file found on GitHub for a chatbot.

import openai

openai.api_key = "placeholder"

messages = []
system_msg = input("What type of chatbot would you like to create?\n")
messages.append({"role": "system", "content": system_msg})

print("Your new assistant is ready!")
while input != "quit()":
    message = input()
    messages.append({"role": "user", "content": message})
    response = openai.ChatCompletion.create(
        model="gpt-3.5-turbo",
        messages=messages)
    reply = response["choices"][0]["message"]["content"]
    messages.append({"role": "assistant", "content": reply})
    print("\n" + reply + "\n")

I have the openai package installed, as it shows up when typing pip list, and I have also tried various other methods like installing using pip3 instead of pip, but the error remains the same. I have also looked at other posts online with people encountering the same problem, but even then no answers have actually worked so far.

1

There are 1 best solutions below

0
Aswindanu Anwar On

Usually this is happen due to the improper installation. Uninstall then reinstall will solve.

pip uninstall openai

Then upgraded pip.

pip install --upgrade pip

Then re-installed the openapi package.

pip install openai