Fix Python 'cannot import name Anthropic' Error
Updated 8/20/2026
When setting up the Claude API in Python, developers frequently encounter the exception: ImportError: cannot import name 'Anthropic' from 'anthropic'. This error blocks your script from executing and prevents the initialization of the Claude client.
This error is almost never caused by a bug in Anthropic's official library. Instead, it occurs due to Python module namespace shadowing, outdated packages, or environment discrepancies. Follow these troubleshooting steps to identify and resolve the import failure.
Step 1: Check for Local File Shadowing The most common cause of this error is naming your own script file `anthropic.py` or naming your project folder `anthropic`. When you write `from anthropic import Anthropic`, Python searches your local project directory before searching your installed global packages. If it finds a local file named `anthropic.py`, it imports that file instead of the actual SDK.
- Open your project folder.
- Look for any files named anthropic.py or folders named anthropic (other than the official package inside site-packages).
- Rename your local file to something generic, like claude_test.py or api_demo.py.
- Search your folder for any compiled cache files like anthropic.pyc or a __pycache__ folder containing an anthropic reference, and delete them.
- Run your script again.
Step 2: Upgrade the Anthropic Python SDK If you are running an outdated version of the SDK, the `Anthropic` client class may not exist, or the internal file structure of the library might be corrupted.
1. Open your terminal or command prompt. 2. Run the following command to upgrade the library to the latest stable release: `bash pip install --upgrade anthropic ` 3. Verify the installed version by running: `bash python -c "import anthropic; print(anthropic.__version__)" ` 4. Ensure the printed version matches the current active release on PyPI.
Step 3: Resolve Virtual Environment Discrepancies If you installed the `anthropic` package using `pip` globally but are running your script inside a virtual environment (or vice versa), Python won't be able to access the module correctly.
1. Check which Python executable is currently running by adding this to the very top of your script: `python import sys print(sys.executable) ` 2. Run your script and note the printed path. 3. Verify if this path matches the environment where you ran the pip install command. 4. To guarantee installation in the exact environment running your script, install the SDK directly through the active Python interpreter using: `bash python -m pip install anthropic `
Step 4: Clean Up Conflicting Namespace Packages If you previously installed legacy wrappers, community-made Anthropic packages, or beta versions of the SDK, they may be polluting the namespace.
1. Uninstall any potential conflicting packages: `bash pip uninstall anthropic anthropic-beta claude-api ` 2. Reinstall the clean, official package: `bash pip install anthropic ` 3. Ensure your import statement in your script matches the modern syntax exactly: `python from anthropic import Anthropic client = Anthropic() `
When to escalate If you have renamed all shadowing files, verified your virtual environment, reinstalled the package via `python -m pip`, and still receive the `ImportError`, the issue may be a bug with your specific operating system's Python pathing. Visit the [Anthropic Python SDK GitHub repository](https://github.com/anthropics/anthropic-sdk-python) and check the "Issues" tab to see if there is an active bug report matching your specific OS and Python version, or to file a new issue.
Quick fixes
- Claude is down or not loading
- Claude Pro billing or payment problem
- Can't sign in to Claude