ImportError: cannot import name 'OpenAI' from partially initialized module 'openai'
ImportError: cannot import name 'OpenAI' from partially initialized module 'openai' (most likely due to a circular import)Short answer
This error almost never means the SDK is broken. Python found a file or folder named openai in your own project before it reached the installed package, so it imported your file instead. Rename it, delete its compiled cache, and run again. Python itself puts your script folder first on the module search path.
Fix it fast
1Find out which cause you have
Ask Python which file it actually loaded as openai. If the path is inside your project and not in site-packages, you have found it.
python -c "import openai; print(openai.__file__)"2Match what you see, then jump to its fix
- 1
The error path in the traceback points at your own openai.py, and the file imports from openai itself.Your script, or a file beside it, is named openai.pyFix - 2
The script itself has another name, but the error appears only when you start it from that folder.You run python -m or python -c from a folder that contains openai.pyFix
- 1
What does the error look like?
ImportError: cannot import name 'OpenAI' from partially initialized module 'openai' (most likely due to a circular import)
Cause 1 of 2
Your script, or a file beside it, is named openai.py
1Check
Python puts the directory of the script you run at the start of the search path, ahead of installed packages, so a same-named file there wins. Look for the file and for a folder called openai.
ls -la openai.py openai/ 2>/dev/null2Fix
Rename the file (for example to chat_demo.py), remove the cached copy, and run it again.
mv openai.py chat_demo.py
rm -rf __pycache__3Verify
The import now resolves to site-packages.
python -c "from openai import OpenAI; print('ok')"Confirmed by docs.python.org
Cause 2 of 2
You run python -m or python -c from a folder that contains openai.py
1Check
With -m and -c the current directory is added to the start of sys.path, so a file in the directory you are standing in shadows the package.
python -c "import sys; print(sys.path[:2])"2Fix
Rename the file as above. As protection, Python 3.11 and later can skip that extra path entry with the -P option or the PYTHONSAFEPATH variable.
python -P -c "import openai; print(openai.__file__)"3Verify
The printed path points into site-packages.
python -P -c "import openai; print(openai.__file__)"Confirmed by docs.python.org
How do I stop it coming back?
- Never name your own files after a package you import: openai.py, random.py, json.py and similar.
- Name examples after what they do, such as chat_demo.py.
- Check an unexpected import with print(module.__file__) before searching further.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- stegnor.comMy company site: 804 pages from a Python and Jinja generator, with 15 free browser tools.
- MenuloA restaurant discovery platform in Germany, kept current by a Python ingestion pipeline.
- ShopGoAn e-commerce aggregator with a Python scraping engine behind its catalog, past 10K Google Play downloads.
Related fixes
AI
Azure OpenAI says the API deployment for this resource does not exist
The API deployment for this resource does not exist
Azure OpenAI does not take a model name in the model field.
2 causes, each with a fixAI
Azure OpenAI fails with an SSL certificate verification error
httpx.ConnectError: [SSL: CERTIFICATE_VERIFY_FAILED]
The client checks the endpoint's certificate against its CA bundle, which is certifi by default.
2 causes, each with a fixAI
The OpenAI client says the API key is missing
Missing credentials. Please pass an `api_key`
OpenAI() looks for an api_key argument first and then the OPENAI_API_KEY environment variable.
3 causes, each with a fixAI
OpenAI says the model is a chat model and not supported in the v1/completions endpoint
This is a chat model and not supported in the v1/completions endpoint
The v1/completions endpoint accepts completion models such as gpt-3.5-turbo-instruct.
3 causes, each with a fixAI
Old openai.ChatCompletion or openai.Embedding code breaks on a new SDK
APIRemovedInV1
The Python SDK was rewritten in version 1.0.
2 causes, each with a fixAI
TypeError when you index an OpenAI response like a dictionary
TypeError: 'ChatCompletion' object is not subscriptable
Since SDK 1.0 the OpenAI Python library returns Pydantic model objects instead of dictionaries, so response['choices'] fails.
2 causes, each with a fix