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

After the latest OpenAI deprecations in early Jan this year, I’m trying to convert from the older API calls to the newer ones. (openai==0.28.0 to 1.10.0)

After switching to the new functions I always get one error: ImportError: cannot import name ‘OpenAI’ from ‘openai’.

I’m working on an AWS EC2 instance, and I’ve tried to re-install the openai package, and upgrade it.

None of these fixes worked.


from openai import OpenAI
client = OpenAI()

def openai_call(content):
    response =
                 "role": "user",
                 "content": f"{content}"
    return response.choices[0].message.content


Feb 01 18:14:33 ip-172-31-39-220 gunicorn[1641]:   File "/home/ubuntu/code_src/", line 9, in <module>
Feb 01 18:14:33 ip-172-31-39-220 gunicorn[1641]:     import utils
Feb 01 18:14:33 ip-172-31-39-220 gunicorn[1641]:   File "/home/ubuntu/code_src/", line 10, in <module>
Feb 01 18:14:33 ip-172-31-39-220 gunicorn[1641]:     from openai import OpenAI
Feb 01 18:14:33 ip-172-31-39-220 gunicorn[1641]: ImportError: cannot import name 'OpenAI' from 'openai' (/home/ubuntu/code_src/venv/lib/python3.10/site-packages/openai/

I tried to re-install and upgrade the packages, my current openai version is 1.10.0, but still it doesn’t seem to work.

Attempt 1: pip install --upgrade openai==1.10.0 Attempt 2: pip uninstall openai pip install openai==1.10.0

None of this seems to work. When I run it on a python interpreter shell with version 3.10.12, it works, along with the functions. When I try it on my files on the flask (gunicorn) server, it stops working. Does anyone know why?

Please let me know how I can fix it.

Why don’t you try to upgrade the package without specifying a specific version, like this:
pip install -U openai

1 Like

Hi! I tried it, but still no results.

I’ve tried almost all the fixes on the net right now, but I haven’t gotten many promising results.

It works on my shell, along with local, but not on flask for some reason.

Please let me know

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")
    print("OpenAI version is compatible.")

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


Thanks to him :

1 Like