Cannot import openai library despite installation

I’m having trouble with importing the openai package in my Python script. I used pip install openai to install it on my computer and even tried installing it again in the same directory as my Python file.

Whenever I try to execute my script, I keep getting this error: ImportError: No module named openai

Here’s my basic test code:

import openai

openai.api_key = MY_SECRET_KEY

user_prompt = "Tell me this works"

api_response = openai.Completion.create(
    model="text-davinci-002", 
    prompt=user_prompt, 
    max_tokens=10
)

print(api_response)

I’m not sure what I’m missing here. The installation seemed to work fine but Python can’t find the module. Any ideas what could be causing this issue?

maybe there’s a problem with your python enviroment? try creating a virtual env and install openai there. sometimes global installs can be tricky. also, double check your import statement for any typos.

your IDE’s probably using a different python interpreter than your terminal. in VSCode or PyCharm, check the interpreter settings - they usually default to system python while you installed openai somewhere else. quick test: run import sys; print(sys.executable) in both your script and terminal to see if they match.