Generate UUID v4 and v7 in Python
Copy-paste Python code that produces a UUID v4 (random, the long-standing default) or UUID v7 (time-ordered, the modern choice for database keys). Both blocks below are production-ready.
UUID v4 in Python
v4 is purely random — 122 bits of entropy, no structure. Use it when you don't want timing information embedded in the ID, or when you don't care about index locality.
import uuid
# UUID v4 — purely random
print(uuid.uuid4())
# Example: 9e0c2a4b-8d3f-4b6e-9c1a-1f2b3c4d5e6f
UUID v7 in Python
v7 embeds a millisecond Unix timestamp in the first 48 bits, so the IDs sort chronologically. This is what modern databases want from a primary key — sequential inserts land in adjacent index pages instead of scattering writes.
# Standard library 'uuid' adds v7 in Python 3.14+
# For earlier versions, use the 'uuid7' package or 'uuid_utils':
# pip install uuid7
import uuid7
print(uuid7.uuid7())
# Example: 0193a0b8-3e7e-7c91-9d2a-5f6a7b8c9d0e
Library support
UUID v7 was added to the standard `uuid` module in Python 3.14 (October 2025). On 3.13 and earlier, install `uuid7` or `uuid-utils` from PyPI.
Notes
The standard library's `uuid` module uses `os.urandom()` under the hood — the same source CPython uses for cryptographic randomness, so v4 output is fine for security-sensitive identifiers.
Need them in bulk?
Our UUID generator produces 1 to 1000 UUIDs (v4 or v7) instantly, all in your browser, with one-click copy and JSON/CSV export. Useful for seeding test data or pre-generating IDs offline.