Cannot import OpenAI- python


from pathlib import Path
from openai import OpenAI

client = OpenAI(apikey='■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■IyBjnByu')
speech_file_path = Path(__file__).parent / "speech.mp3"
response = client.audio.speech.create(
    model="tts-1",
    voice="alloy",
    input="Your text goes here!"
)
response.stream_to_file(speech_file_path)
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
Cell In[16], line 2
      1 from pathlib import Path
----> 2 from openai import OpenAI
      4 client = OpenAI(apikey='■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■IyBjnByu')
      5 speech_file_path = Path(__file__).parent / "speech.mp3"

ImportError: cannot import name 'OpenAI' from 'openai' (C:\Users\XXXXXX\AppData\Local\anaconda3\Lib\site-packages\openai\__init__.py)

Also i have already done !pip install --upgrade openai

Try this to double check you actualy updated in the right environment:

from openai import OpenAI, __version__
print(__version__)

You likely have multiple installations of python going on, perhaps a user installation “anaconda3” and a system install.

You’ll need to do a “conda install -c conda-forge openai” as user to get that version upgraded.

1 Like