Python uuid: uuid4 for random IDs, uuid5 for name-based IDs, uuid7 for time-ordered IDs
Answer: Call uuid.uuid4() for a random UUID generated in a cryptographically-secure method. Use uuid.uuid5(namespace, name) for a repeatable ID derived from a name, and uuid.uuid7() (Python 3.14+) for time-based UUIDs. Avoid uuid1() when privacy matters, because it contains the computer's network address.
Examples
import uuid
uuid.uuid4() # random
uuid.uuid5(uuid.NAMESPACE_DNS, "python.org") # same input, same UUID
uuid.uuid7() # time-based, 3.14+
u = uuid.UUID("{12345678-1234-5678-1234-567812345678}")
u.hex, u.int, u.urn, u.version
python -m uuid # uuid4 by default
python -m uuid -u uuid5 -n @dns -N example.com
Versions
| Function | Based on | |---|---| | uuid1() | host ID, sequence number, and the current time | | uuid3() / uuid5() | MD5 / SHA-1 hash of a namespace UUID and a name | | uuid4() | random | | uuid6() | sequence number and current time; improves database locality over uuid1() | | uuid7() | 48-bit timestamp plus a 42-bit counter for monotonicity within a millisecond | | uuid8() | pseudo-random, with optional custom bits |
UUID version 2 is deliberately omitted. Versions 6, 7 and 8 were added in Python 3.14.
Details
- Namespaces include
NAMESPACE_DNS(a fully qualified domain name),NAMESPACE_URL,NAMESPACE_OIDandNAMESPACE_X500. hexis a 32-character lowercase hexadecimal string;intis a 128-bit integer.is_safereports whether the platform generated the UUID in a multiprocessing-safe way.getnode()may launch a separate program the first time it runs, which could be quite slow.- The command-line interface was added in Python 3.12.
See also
Claims
- id: C1 claim: uuid4 generates a random UUID in a cryptographically-secure method. source: S1 evidence: "Generate a random UUID in a cryptographically-secure method"
- id: C2 claim: uuid1 may compromise privacy. source: S1 evidence: "Note that uuid1() may compromise privacy"
- id: C3 claim: uuid7 uses a 42-bit counter for monotonicity. source: S1 evidence: "use a 42-bit counter to guarantee monotonicity within a millisecond"
- id: C4 claim: uuid4 generates a random UUID. source: S2 evidence: "\"\"\"Generate a random UUID.\"\"\""
Sources
- [S1] uuid — UUID objects according to RFC 9562. Python docs. https://docs.python.org/3/library/uuid.html . License: PSF-2.0. Accessed 2026-11-18.
- [S2] Lib/uuid.py. python/cpython (GitHub). https://github.com/python/cpython/blob/main/Lib/uuid.py . License: PSF-2.0. Accessed 2026-11-18.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Python docs, uuid — UUID objects according to RFC 9562; python/cpython (GitHub), Lib/uuid.py. PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation. Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Python docs, uuid — UUID objects according to RFC 9562; python/cpython (GitHub), Lib/uuid.py. PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation.
Cite this
iinventedlight. 2026. "Python uuid: uuid4 for random IDs, uuid5 for name-based IDs, uuid7 for time-ordered IDs." iinventedlight Library, iil:note:s2qqk322, version of 2026-11-18. https://iinventedlight.com/library/notes/python-uuid/. License: CC-BY-4.0.
@misc{iil_note_s2qqk322,
author = {{iinventedlight}},
title = {{Python uuid: uuid4 for random IDs, uuid5 for name-based IDs, uuid7 for time-ordered IDs}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-18},
url = {https://iinventedlight.com/library/notes/python-uuid/},
note = {Permanent ID iil:note:s2qqk322. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
Extra files beside this note
These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.
- python-uuid.table.json (table; CC-BY-4.0)
- python-uuid.schema.json (schema; CC-BY-4.0)
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/python-uuid.md.