ImportError: cannot import name 'OpenAI' from 'openai'

This is something that happened to me, and here’s what worked for me ( I’m not saying it will work for you. )

When I was installing the dependencies for my project, in the dotenv repos, the user didn’t have write permissions in the dotenv, so python was installing the dependencies in python’s .bin folder by default, which meant that when I launched my project, the dependencies weren’t where they were supposed to be in the dotenv causing this error.

Try writing a script (not using dotenv) that checks that OpenAI is installed with the correct version. If it works with python3 but not in your project, there’s a good chance that ‘chmod 777 -R *’ on your project and reinstalling the dependencies will work for you.

The script that may help you

import openai
from packaging import version

required_version = version.parse("1.1.1") # replace the version by the version you want
current_version = version.parse(openai.__version__)

if current_version < required_version:
    raise ValueError(f"Error: OpenAI version {openai.__version__}"
                     " is less than the required version 1.1.1")
else:
    print("OpenAI version is compatible.")

# -- Now we can get to it
from openai import OpenAI

print('OPENAI WAS GREAT AGAIN')

Thanks to him : https://community.openai.com/t/cannot-import-name-openai-from-openai/486147/6

1 Like