← Blog

Anthropic Claude SDK Python: Install Guide

2026-10-14 · 5 min read · SubToAPI Team

Installing the Anthropic Claude SDK for Python

To install the official Anthropic Claude SDK for Python, run pip install anthropic inside a virtual environment, then set your API key as an environment variable and instantiate the client with anthropic.Anthropic(). That's the whole installation process — no compiled dependencies, no system-level packages, no extra configuration files.

The rest of this guide walks through the setup in detail: creating a clean environment, installing the package correctly, handling your API key safely, and making your first call to the Messages API. It also covers common installation errors and what to do if you want a single HTTPS endpoint instead of managing SDK versions and key rotation yourself.

Prerequisites

Before installing, make sure you have:

If you're on an older Python version, the SDK will fail to install cleanly or throw import errors at runtime. Upgrading Python first avoids most installation headaches.

Step 1: Create a Virtual Environment

Installing directly into your system Python is a common source of dependency conflicts. Use a virtual environment instead:

python3 -m venv .venv
source .venv/bin/activate   # On Windows: .venv\Scripts\activate

Your shell prompt should now show (.venv), confirming the environment is active.

Step 2: Install the Package

With the environment active, install the SDK:

pip install anthropic

This pulls in the anthropic package along with its dependencies (httpx, pydantic, and a few typing utilities). Installation typically takes a few seconds and doesn't require a compiler.

To confirm it worked:

python3 -c "import anthropic; print(anthropic.__version__)"

If this prints a version number without errors, the SDK is installed correctly.

Pinning a version (recommended for production)

For reproducible builds, pin the version in your requirements.txt:

anthropic==0.34.0

Check the current release on PyPI before pinning, since the SDK is updated frequently with new model support and bug fixes.

Step 3: Set Your API Key

The SDK reads your key from the ANTHROPIC_API_KEY environment variable by default. Set it in your shell:

export ANTHROPIC_API_KEY="your-key-here"

Or load it from a .env file using python-dotenv:

pip install python-dotenv
from dotenv import load_dotenv
load_dotenv()

Never hardcode the key in source files, and never commit .env files to version control. Add .env to your .gitignore before your first commit.

Step 4: Make Your First Request

Once the key is set, a basic request looks like this:

from anthropic import Anthropic

client = Anthropic()  # reads ANTHROPIC_API_KEY automatically

message = client.messages.create(
    model="claude-sonnet-4-20250514",
    max_tokens=1024,
    messages=[
        {"role": "user", "content": "Explain what a virtual environment does."}
    ]
)

print(message.content[0].text)

Run the script. If it returns a text response, your installation is working end-to-end — package installed, key authenticated, request successful.

Common Installation Problems

ModuleNotFoundError: No module named 'anthropic' Usually means the package installed into a different Python environment than the one running your script. Confirm your virtual environment is active and that pip and python3 point to the same environment with which pip and which python3.

AuthenticationError on first request The SDK installed fine, but the environment variable isn't set in the shell that's running your script. If you're using an IDE, restart it after setting the variable, or pass the key explicitly: Anthropic(api_key="...").

SSL or certificate errors Usually a corporate proxy or outdated certifi package. Run pip install --upgrade certifi and retry.

Version conflicts with other packages If another library pins an incompatible httpx or pydantic version, isolate Claude SDK usage in its own virtual environment or use pip-tools / poetry to resolve dependency trees properly.

Streaming and Async Support

The SDK also installs an async client out of the box — no separate package needed:

from anthropic import AsyncAnthropic
import asyncio

async def main():
    client = AsyncAnthropic()
    async with client.messages.stream(
        model="claude-sonnet-4-20250514",
        max_tokens=1024,
        messages=[{"role": "user", "content": "Write a haiku about pip install."}]
    ) as stream:
        async for text in stream.text_stream:
            print(text, end="", flush=True)

asyncio.run(main())

This is useful for chat interfaces or any workflow where you want tokens to appear incrementally rather than waiting for the full response.

Managing the SDK Across Multiple Services

If you're building more than a single script — a backend with multiple services, a team of developers, or an app that needs usage tracking per customer — raw SDK installs start to show their limits. You end up managing API key rotation, tracking usage per project manually, and writing your own retry and rate-limit handling around the SDK calls.

SubToAPI sits on top of this layer: it turns your existing Claude access into an HTTPS API with application-scoped keys (sub_live_...), built-in streaming, tool use support, and usage metadata per key, so you're not re-implementing that plumbing in every service that calls Claude. The API shape mirrors the Messages API closely, so switching an existing integration over is usually a small diff rather than a rewrite — see the quickstart and Messages API docs for the exact request format.

If you only need a single script talking to Claude, the official anthropic package installed above is all you need. If you're coordinating API access across a team or multiple apps, it's worth comparing plans on the pricing page.

Questions

Does installing the Anthropic SDK require anything besides pip? No. pip install anthropic is sufficient on Python 3.8+. There are no system-level dependencies or compiled extensions to install separately.

Can I use the Claude SDK without setting an environment variable? Yes — pass the key directly when instantiating the client: Anthropic(api_key="your-key"). Environment variables are recommended for security, but not required by the SDK itself.

Why does my script raise an import error even after installing the package? This almost always means pip installed into a different Python environment than the one executing your script. Activate your virtual environment and verify which python3 and which pip point to the same path before reinstalling.

Turn your Claude access into an HTTPS API

SubToAPI gives you application API keys, streaming, tool use and usage insights on top of your existing Claude access — set up in minutes.

Start free  Read the quickstart →