Get started with the Python SDK

이 페이지는 아직 귀하의 언어로 번역되지 않았습니다. 현재 작업 중이므로 곧 다시 확인해 주십시오.

For Python developers10 minBeginner

This guide helps you install the Amplitude Python SDK (amplitude-analytics), create a client, and send one event to Amplitude from a server, script, or worker. Each step lists the problems that commonly break it, so you can fix a problem where it happens. For the full option list, go to Configuration options. For changes between versions, go to the Amplitude-Python changelog.

Prerequisites

  • An Amplitude project and its API key. To find the key, go to Settings > Projects, select your project, and open General.
  • The data center your organization uses: US (default) or EU.
  • Python 3.6 or later. The package declares python_requires=">=3.6, <4" and has no third-party dependencies.
  • Outbound HTTPS access from your server to api2.amplitude.com (US) or api.eu.amplitude.com (EU).

Install the SDK

Install the package with the tool your project uses. The distribution name is amplitude-analytics, and the module you import is amplitude.

bash
pip install amplitude-analytics

Fix installation problems

Initialize the SDK

Create one Amplitude client per process and reuse it for every request. Each client starts its own background threads, which batch events and upload them.

python
from amplitude import Amplitude

client = Amplitude("AMPLITUDE_API_KEY")

If your project uses the EU data center, pass Config(server_zone="EU") as the second argument. Go to Choose a data region.

The SDK reports problems through Python's standard logging module, on the logger named amplitude. By default, Python shows only WARNING and higher messages. To see each upload result while you set up the SDK, turn on INFO:

python
import logging

logging.basicConfig(level=logging.INFO)

Fix initialization problems

Send a first event

Track one event for a user, then shut the client down. shutdown() sends every queued event and waits for the upload to finish. Call it at the end of a short script. A long-running server keeps the client open instead. Pass a callback to see the upload result for each event.

python
from amplitude import Amplitude, BaseEvent, Config

def on_result(event, code, message):
    print(event.event_type, code, message)

client = Amplitude("AMPLITUDE_API_KEY", Config(callback=on_result))
client.track(
    BaseEvent(
        event_type="Sign Up Completed",
        user_id="user-12345",
        event_properties={"plan": "starter"},
    )
)
client.shutdown()
# Sign Up Completed 200 Event sent successfully.

Every event needs a user_id or a device_id, and Amplitude rejects IDs shorter than five characters. Without a shutdown() call, the SDK still flushes when the interpreter exits normally, because the SDK registers shutdown() as an exit handler.

Fix upload problems

The callback receives an integer code and a string message. Some problems never reach the callback, so the first column also lists log lines.

Confirm the event in Amplitude

  1. In Amplitude, open User Lookup and search for the user ID you sent, user-12345 in this example.
  2. Open the Activity tab and turn on Live event updates.
  3. Confirm that Sign Up Completed appears with the plan property.

If the event doesn't appear within a minute, follow Verify events. That guide walks through each stage between track() and Amplitude's servers.

Next steps

이 내용이 도움이 되었나요?

For AI agents: a documentation index is available at /docs/llms.txt. Append .md to any page URL for markdown, or send Accept: text/markdown.

이 페이지에서