UUID Generator for Python

Python has a built-in uuid module (since Python 2.5) that generates RFC 4122 UUIDs. No extra packages are required for v1 and v4. Here's how to use it.

Version 4 (Random)

For a random UUID, use uuid.uuid4(). This is the most common choice for new records, session IDs, and tokens.

import uuid

id = uuid.uuid4()
print(id)           # e.g. 550e8400-e29b-41d4-a716-446655440000
print(str(id))      # same as string
print(id.hex)       # 32 hex chars, no hyphens

Version 1 (Time-Based)

uuid.uuid1() uses the host ID and current time. Useful when you want time-ordered or machine-identifiable IDs.

id = uuid.uuid1()

Version 5 (Name-Based)

For deterministic UUIDs from a namespace and name, use uuid.uuid5(namespace, name). Same inputs always yield the same UUID.

# Namespace DNS for domain-based IDs
id = uuid.uuid5(uuid.NAMESPACE_DNS, 'example.com')

# Or use a custom namespace UUID
ns = uuid.UUID('6ba7b810-9dad-11d1-80b4-00c04fd430c8')
id = uuid.uuid5(ns, 'user@example.com')

Nil UUID

uuid.UUID('00000000-0000-0000-0000-000000000000') or use our generator and select Nil for the constant empty UUID.

Best Practices

Need UUIDs from an HTTP API instead? Use our API. For bulk generation in the browser, use our bulk generator.

When to use v4 vs v5 · Bulk UUID for testing