Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
AI and LLM integration

ImportError: cannot import name 'OpenAI' from partially initialized module 'openai'

ErrorImportError: 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.

  • Applies to Python 3.11+, openai Python SDK 1.x
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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__)"
  2. 2Match what you see, then jump to its fix

    1. 1The 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. 2The 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

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/null

2Fix

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

Did this fix it?

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

Did this fix it?

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:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.