uuid — UUID objects

uuid.UUID and uuid5 deterministic values on Python 3.13, and why uuid4 is random rather than a secret.

The uuid module builds RFC 4122 UUID objects. Import it as import uuid. uuid5(namespace, name) is a name-based UUID and is deterministic for the same namespace and name. The example below uses the official documentation pair NAMESPACE_DNS and "python.org", which yields 886313e1-3b8a-5372-9b90-0c9aee199e5d, and then reconstructs that value with the UUID constructor.

uuid4() generates a random UUID. It is not a password, an API secret, or an authentication token, and a printed uuid4() value would not be a stable expected output, so this page does not call it. Python 3.13 does not provide uuid7; the examples here stay with the 3.13 API.

Examples were run against Python 3.13.12. Links point to the Python 3.13 documentation.

Examples were checked against Python 3.13. See the official documentation for the complete API.

Python reference overview →

uuid5 and UUID

uuid5 hashes a namespace UUID and a name with SHA-1. The version of the result is 5. Passing the same hex string to uuid.UUID rebuilds the same object, which is why u == fixed is True. The constructor can also take bytes or an integer; those forms are omitted here.

uuid.uuid5 and uuid.UUID

uuid.uuid5(namespace, name)
uuid.UUID(hex=None, bytes=None, bytes_le=None, fields=None, int=None, version=None, *, is_safe=SafeUUID.unknown)

Build the documented UUID5 for python.org and parse the same value from a hex string.

ParameterDescription
namespace, nameA UUID namespace, such as uuid.NAMESPACE_DNS, and the name string. The pair used here is the one from the Python 3.13 documentation.
hexA 32-digit hex string, with or without hyphens, used by the UUID constructor.

Returns: A UUID object. version is 5 for uuid5. hex is the 32-character form without hyphens.

Raises

  • ValueError — Raised by UUID when the hex string is the wrong length or is not hexadecimal.
  • The UUID constructor has several mutually alternative inputs. This example only uses hex. is_safe is shown in the official signature and is not used here.
python
import uuid

u = uuid.uuid5(uuid.NAMESPACE_DNS, "python.org")
print(u)
print(u.version)
fixed = uuid.UUID("886313e1-3b8a-5372-9b90-0c9aee199e5d")
print(fixed)
print(fixed.hex)
print(u == fixed)

Expected output

886313e1-3b8a-5372-9b90-0c9aee199e5d
5
886313e1-3b8a-5372-9b90-0c9aee199e5d
886313e13b8a53729b900c9aee199e5d
True

Related: hashlib

Official documentation →