uuid.uuid3 / uuid5
The hash of namespace.bytes + name (a str is encoded as UTF-8), cut to 16 bytes, with the version and variant bits set. No randomness: anyone with the same namespace and name computes the same UUID. Prefer uuid5; uuid3 exists for compatibility.
Demo
import uuid (uuid.uuid3(uuid.NAMESPACE_DNS, 'python.org'), uuid.uuid5(uuid.NAMESPACE_DNS, 'python.org'))
python.org reproduces the two UUIDs printed in the Python docs. Python.org gives unrelated values: the hash sees different bytes. Non-ASCII names work because a str is encoded to UTF-8 first. The third tab shows that uuid5 does no normalization: a trailing slash or a capital letter is a different name, even where a browser would treat the URLs as the same. Normalize names yourself before hashing.
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
| namespace | uuid.UUID | yes | A UUID that scopes the names: one of NAMESPACE_DNS/URL/OID/X500, or your own (e.g. a uuid4 you generated once). |
| name | str | bytes | yes | The name to hash. str is encoded with UTF-8; bytes are used as they are. |
Return value
uuid.UUID — Version 3 (MD5) or version 5 (SHA-1), RFC 4122 variant.
Common patterns
import uuid APP_NS = uuid.UUID('1cc262f7-3046-59d8-af1f-724f1ed6b671') # generated once, then fixed record_id = uuid.uuid5(APP_NS, f'customer:{external_id}')
import uuid key = uuid.uuid5(uuid.NAMESPACE_DNS, hostname.strip().lower().rstrip('.'))
import uuid USERS = uuid.uuid5(uuid.NAMESPACE_URL, 'https://example.com/users') user_id = uuid.uuid5(USERS, str(42))
Examples
Pitfalls
import uuid uuid.uuid5('6ba7b810-9dad-11d1-80b4-00c04fd430c8', 'python.org')
import uuid uuid.uuid5(uuid.UUID('6ba7b810-9dad-11d1-80b4-00c04fd430c8'), 'python.org')
import uuid uuid.uuid5(uuid.NAMESPACE_DNS, 42)
import uuid uuid.uuid5(uuid.NAMESPACE_DNS, str(42)).version
import uuid uuid.uuid5(uuid.NAMESPACE_DNS, 'admin') == uuid.uuid5(uuid.NAMESPACE_DNS, 'admin')
import secrets len(secrets.token_urlsafe(32))
When to use
- The same input must map to the same ID on every machine and every run
- Idempotent imports and deduplication
- IDs for URLs, domain names, file paths, external keys
- IDs that must not be guessable → uuid4 or secrets
- New code choosing between the two → uuid5 (SHA-1) rather than uuid3 (MD5); RFC 4122 recommends version 5
Notes
FAQ
uuid.uuid5(namespace, text), for example uuid.uuid5(uuid.NAMESPACE_URL, 'https://example.com/'). The same namespace and text always give the same UUID, on any machine and any Python version.