OpenAI import not working

Hi trying to use the very simpel example on the documentation site

from openai import OpenAI
client = OpenAI()

completion = client.chat.completions.create(
  model="gpt-3.5-turbo",
  messages=[
    {"role": "system", "content": "You are a poetic assistant, skilled in explaining complex programming concepts with creative flair."},
    {"role": "user", "content": "Compose a poem that explains the concept of recursion in programming."}
  ]
)

print(completion.choices[0].message)

But everytime i get the same error :ModuleNotFoundError: No module named ‘openai’.
my set up is a virtualenviroment that is active with a openai version 1.12.0 and python version 3.12.2. why oh why cant my python program find the package.

Assuming everything is correctly installed, you might look at your paths to ensure python can see where openai is installed.

1 Like

Please ensure that your Python virtual environment is activated, and try the command:
pip show openai

If the OpenAI module is installed correctly, you will see a display like this:

Name: openai
Version: 1.12.0
Summary: The official Python library for the openai API
Home-page:
Author:
Author-email: OpenAI support@openai.com
License:
Location: {your path location goes here}
Requires: anyio, distro, httpx, pydantic, sniffio, tqdm, typing-extensions
Required-by:

If you don’t see the display like above, please check whether the virtual environment is active or if the OpenAI module is installed within the virtual environment.

1 Like