---
source: https://iinventedlight.com/library/notes/python-uuid.md
title: "Python uuid: uuid4 for random IDs, uuid5 for name-based IDs, uuid7 for time-ordered IDs"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "PSF-2.0 (Python docs); PSF-2.0 (python/cpython (GitHub)); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "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."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-18
status: published
approved_by: "Cole Withington, 2026-11-20 13:14 PT: \"Publish the full batch and rewrite the 6 live notes\""
checked_on: 2026-11-18
as_of: 2026-11-18
confidence: medium
untested: "Not reproduced (2026-11-18). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-python: uuid"
tags: [python, uuid, identifiers, rfc-9562, stdlib]
client: Librarian
sources:
  - id: S1
    title: "uuid — UUID objects according to RFC 9562"
    publisher: Python docs
    url: https://docs.python.org/3/library/uuid.html
    accessed: 2026-11-18
    license: "PSF-2.0"
  - id: S2
    title: "Lib/uuid.py"
    publisher: python/cpython (GitHub)
    url: https://github.com/python/cpython/blob/main/Lib/uuid.py
    accessed: 2026-11-18
    license: "PSF-2.0"
---
# 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

```python
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
```

```shell
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_OID` and `NAMESPACE_X500`.
- `hex` is a 32-character lowercase hexadecimal string; `int` is a 128-bit integer.
- `is_safe` reports 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

- [python-secrets](https://iinventedlight.com/library/notes/python-secrets/)
- [python-dataclasses](https://iinventedlight.com/library/notes/python-dataclasses/)
- [python-json](https://iinventedlight.com/library/notes/python-json/)

## 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.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
