OpenAI Python Import Error: How to Fix
Updated 10/28/2026
When integrating OpenAI's API into Python applications, developers frequently encounter errors like ImportError: cannot import name 'OpenAI' from 'openai' or ModuleNotFoundError: No module named 'openai'.
These integration failures are almost always caused by outdated library versions, namespace collisions (such as naming your local script openai.py), or configuration mismatches following OpenAI's major v1.0.0 SDK rewrite.
Use this practical troubleshooting guide to resolve Python import and environment configuration issues.
Why OpenAI Import Errors Occur
In late 2023, OpenAI released version 1.0.0 of its Python SDK, introducing structural breaking changes. The legacy interface, which relied on global configuration module variables, was replaced by an explicit, client-based interface.
If your environment runs legacy code on a modern SDK version—or modern code on an outdated package—your scripts will throw fatal import or attribute errors.
Agent Builder turns a task you repeat into a reusable assistant with setup text for each app.
Tickd is independent and not affiliated with any AI company.
How to Fix OpenAI Python Import Errors
Follow these sequential steps to clean up your environment and correct your SDK implementation.
1. Upgrade or Downgrade the SDK Package First, ensure you have the correct version of the package installed in your active runtime environment.
* To use the modern client interface (Recommended): Update your package to the latest version by running: `bash pip install --upgrade openai ` * To check your currently installed version: `bash pip show openai ` * To run legacy code without rewriting it: If you must support older codebases, pin your installation to the legacy version: `bash pip install "openai<1.0.0" `
2. Check for File Naming Conflicts A common trap for developers is naming their test script `openai.py`. When your script runs `import openai`, Python checks the local directory first, attempting to import your script instead of the official library.
- Rename your local file from openai.py to something unique (e.g., openai_test.py).
- Check your working directory for any generated compiler files, such as openai.pyc or a __pycache__ folder, and delete them.
- Ensure no folder in your project path is named openai.
3. Update Your Import and Initialization Syntax If you are running OpenAI SDK v1.0.0 or higher, you must instantiate a client object instead of calling global module methods.
* Incorrect Legacy Syntax (fails on v1.0.0+): `python import openai openai.api_key = "your-api-key" # Throws AttributeError: module 'openai' has no attribute 'ChatCompletion' response = openai.ChatCompletion.create(model="gpt-4", messages=[...]) ` * Correct Modern Syntax: `python from openai import OpenAI
The SDK automatically pulls the key from the OPENAI_API_KEY environment variable client = OpenAI()
Or pass it explicitly: # client = OpenAI(api_key="your-api-key")
response = client.chat.completions.create( model="gpt-4o", messages=[{"role": "user", "content": "Hello!"}] ) print(response.choices[0].message.content) `
4. Verify Active Python Environment Paths If you receive a `ModuleNotFoundError` even after running `pip install`, your IDE or system terminal is likely running a different Python environment than the one where the package was installed.
1. Identify the active interpreter: Run this script to locate the active Python executable path: `python import sys print(sys.executable) ` 2. Install directly to that interpreter: Use the absolute path to force installation to the correct location: `bash /path/to/your/active/python -m pip install --upgrade openai ` 3. Virtual Environments: If using venv or conda, ensure you activate the environment *before* installing packages: `bash source myenv/bin/activate # macOS/Linux myenv\Scripts\activate # Windows `
5. Handle Timeout and SDK Connection Configurations If your imports succeed but the integration fails during active network requests, configure timeouts and retries directly on the SDK client initialization to prevent hanging scripts:
`python from openai import OpenAI
client = OpenAI( timeout=20.0, # Set request timeout to 20 seconds max_retries=3, # Set automatic retries for rate limits or server errors ) `
When to Escalate
If you have confirmed that your virtual environment is clean, your syntax matches your SDK version, and you still encounter low-level library failures, check the [OpenAI Python SDK GitHub repository](https://github.com/openai/openai-python) issues page. Since this is an open-source library, bugs related to specific OS platforms or Python minor versions are tracked and resolved there by the maintainers. For account or API platform key authorization issues, log in to the OpenAI Developer Platform and use the built-in Help Center widget.