UUID in Python
Python UUID Generator
Python's uuid module covers the whole of RFC 9562 — and since 3.14 it includes the versions developers were previously pip-installing.
Standard libraryv4, v5, v6, v7, v8NIL and MAX
Generated locally with the Web Crypto API — nothing leaves your browser.
Generating UUIDs in Python
uuid.uuid4()returns a random UUID built fromos.urandom, so it is cryptographically secure and the right default for almost everything. It is aUUIDobject, not a string: callstr()for the hyphenated form, or use.hexfor the 32-character form with no hyphens.- Python 3.14 added
uuid6(),uuid7()anduuid8()as well as theuuid.NILanduuid.MAXconstants, so time-ordered primary keys need no third-party package any more. On 3.13 and earlier theuuid6package fills the same gap.
How to generate
- Press Generate UUID above to mint a value in this browser — nothing is sent anywhere.
- Copy one value with Copy, or set How many and use Copy all for a fixture list.
- Switch Version if your code needs v7 instead of the v4 default, and turn on Formatting to match your stack's conventions.
Use Cases
- Django primary keys —
UUIDField(default=uuid.uuid4), and v7 keeps the index sequential. - SQLAlchemy models — the same default, mapped as
uuid.UUID. - Deterministic IDs —
uuid5()turns a name into a reproducible identifier for idempotent jobs.
How Python UUID Generator Compares
| uuid.uuid4() | Random, CSPRNG, the safe default |
| uuid.uuid7() | Time-ordered — Python 3.14+, or the uuid6 package |
| uuid.uuid5() | Deterministic from a name; same input, same ID, always |
Code Examples
Python
import uuid
str(uuid.uuid4()) # v4, random -> '65e82210-1a2d-4011-a064-a6a60565aa41'
uuid.uuid7() # v7, time-ordered (Python 3.14+)
uuid.uuid5(uuid.NAMESPACE_DNS, 'example.com') # deterministic
uuid.NIL # 00000000-0000-0000-0000-000000000000 (3.14+)
u = uuid.uuid4()
u.hex # 32 chars, no hyphens
u.bytes # 16 raw bytesDjango
import uuid
from django.db import models
class Order(models.Model):
# Pass the function, NOT uuid.uuid4() — calling it would give every
# row the same value.
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)Frequently Asked Questions
How do I get a UUID v7 in Python?
On Python 3.14 or later,
uuid.uuid7() is in the standard library. On 3.13 and earlier, install the uuid6 package and call uuid6.uuid7().Why do all my rows get the same UUID in Django?
You wrote
default=uuid.uuid4() with parentheses. That evaluates once at import time; pass the function itself — default=uuid.uuid4.Is the Python uuid module cryptographically secure?
uuid4() is — it is seeded from os.urandom. uuid1() embeds the host MAC address and timestamp instead, so avoid it for anything user-visible.Related tools
UUID v1 GeneratorUUID v3 GeneratorUUID v4 GeneratorUUID v5 GeneratorUUID v6 GeneratorUUID v7 GeneratorUUID v8 GeneratorBulk UUID GeneratorShort UUID GeneratorUUID ValidatorUUID FormatterJavaScript UUID GeneratorTypeScript UUID GeneratorNode.js UUID GeneratorReact UUID GeneratorPython UUID GeneratorJava UUID GeneratorC# UUID GeneratorGo UUID GeneratorPHP UUID GeneratorRuby UUID GeneratorRust UUID GeneratorKotlin UUID GeneratorSwift UUID GeneratorSQL UUID Generator