Cannot import name 'OpenAI' from 'openai'. Python 3.12

Hi everyone!

I have the following problem: cannot import name ‘OpenAI’ from ‘openai’
I tried to start this simple python code

from openai import OpenAI
client = OpenAI(
    api_key=api_key
)


def transcribe_audio(audio_file_path):
    with open(audio_file_path, 'rb') as audio_file:
        transcription = client.audio.transcriptions.create("whisper-1", audio_file)
    return transcription['text']


print(transcribe_audio("video1.mp3"))

Python 3.12 version
openai 1.23.2 version

I tried my best to fix it, uninstalled openai lib and then install again, downgrade and update my python version and nothing works. Also I double checked that I don’t have other file with name “openai”.

Please help me

Sounds like an issue with your Python environment. My guess would be that you are installing the openai module into a different environment than the one you are trying to run your code in.

The operating system you are using and installing on, if you have administrator rights, and how you are invoking Python are important pieces of information to solving this.

Here’s a script to answer a lot of questions for you about where python and openai are (or are not) installed and running from, along with versions.

import sys
import importlib.util
import os

def print_python_info():
    # Print Python version
    print("Python Version:")
    print(sys.version)
    print("")

    # Print Python Install Directory
    print("Python Install Directory:")
    print(sys.prefix)
    print("")

    # Print the PATH environment variable
    print("PATH Environment Variable:")
    print(os.getenv("PATH"))
    print("")

    # Print PYTHONPATH if it's set
    print("PYTHONPATH Environment Variable:")
    pythonpath = os.getenv("PYTHONPATH")
    if pythonpath:
        print(pythonpath)
    else:
        print("Not Set")
    print("")

    # Print PYTHONHOME if it's set
    print("PYTHONHOME Environment Variable:")
    pythonhome = os.getenv("PYTHONHOME")
    if pythonhome:
        print(pythonhome)
    else:
        print("Not Set")
    print("")

    # Attempt to import the 'openai' module and print its version and file location
    try:
        # Importing the openai module
        import openai
        
        # Print openai module version if available
        print("OpenAI Module Version:")
        if hasattr(openai, '__version__'):
            print(openai.__version__)
        else:
            print("Version not specified")
        print("")

        # Print location of the openai module
        print("OpenAI Module Location:")
        print(importlib.util.find_spec("openai").origin)
        print("")
    
    except ImportError:
        print("OpenAI module is not installed")
        print("")

if __name__ == "__main__":
    print_python_info()

This alone may give you an “aha!”

1 Like