DevBolt
Processed in your browser. Your data never leaves your device.

Python UUID Generator

Generate UUIDs and test them for your Python projects. Generate random UUIDs instantly, then use the Python code examples for uuid4, uuid1, uuid5, and database integration. All generation happens in your browser.

← Back to tools

UUID Generator

Generate random UUID v4 identifiers. Bulk generation supported.

How to generate UUIDs in Python

Python's uuid module: import uuid; id = uuid.uuid4() generates a random UUID v4. Other versions: uuid.uuid1() (MAC address + timestamp — includes machine identity), uuid.uuid3(uuid.NAMESPACE_DNS, 'example.com') (MD5-based deterministic), uuid.uuid5(uuid.NAMESPACE_DNS, 'example.com') (SHA-1-based deterministic). Access as string: str(uuid.uuid4()) returns '550e8400-e29b-41d4-a716-446655440000'. As hex without dashes: uuid.uuid4().hex. As bytes: uuid.uuid4().bytes (16 bytes).

UUIDs in Python databases

SQLAlchemy: from sqlalchemy import Column; from sqlalchemy.dialects.postgresql import UUID; id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4). Django: from django.db import models; id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False). For PostgreSQL, use the native uuid type. For SQLite/MySQL, store as CHAR(36) or BINARY(16). Performance tip: uuid.uuid4().bytes (16 bytes) is more storage-efficient than the 36-character string representation. Consider UUIDv7 (via uuid6 package) for database-friendly time-sortable IDs.

When to use each UUID version in Python

UUID v4 (uuid.uuid4()): Best general purpose — 122 bits of randomness, no information leakage. Use for database primary keys, API identifiers, session tokens. UUID v1 (uuid.uuid1()): Includes timestamp and MAC address — use when you need time-ordering but note it exposes machine identity. UUID v5 (uuid.uuid5()): Deterministic — same input always produces the same UUID. Use for generating consistent IDs from names (e.g., converting email addresses to UUIDs). UUID v7 (pip install uuid6): Time-sortable random UUIDs — best for database primary keys where insert order matters.

Frequently Asked Questions

What is the difference between uuid1() and uuid4() in Python?

uuid4() generates a random UUID using the OS random number generator — it is the most common choice. uuid1() uses the machine's MAC address and current timestamp, making UUIDs sortable by time but leaking hardware identity. Use uuid4() unless you specifically need time-ordering.

How do I use UUIDs as Django model primary keys?

Use models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False). Note: pass uuid.uuid4 without parentheses — Django calls the function for each new instance. This replaces the default auto-incrementing integer primary key.

Are Python uuid4() UUIDs truly random?

Yes. Python's uuid.uuid4() uses os.urandom() which provides cryptographically secure random bytes from the operating system. With 122 random bits, the probability of collision is negligible — you would need to generate 2.7 quintillion UUIDs to have a 50% chance of one collision.

Related Generate Tools