← All notes

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

See also

Claims

Sources

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

Permanent ID: iil:note:s2qqk322

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.},
}

Markdown

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.

All artifact files (JSON index)

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.