UUID7 Standard (pypi: uuid7-standard)
This library provides a Python implementation for generating and manipulating UUIDs adhering to the final UUID version 7 standard, ensuring time-ordered identifiers. It explicitly distinguishes itself from the older `uuid7` package on PyPI, which was based on an earlier draft of the standard. The current version is 1.1.0, and it follows a release cadence driven by bug fixes and minor improvements.
Common errors
-
ModuleNotFoundError: No module named 'uuid7'
cause This error typically occurs when you've installed `uuid7-standard` but are attempting to import from `uuid7` (e.g., `import uuid7`). The `uuid7` package is a separate, distinct library.fixIf you intend to use the `uuid7-standard` library, ensure your imports are `from uuid7_standard import ...`. If you genuinely meant to use the other package, install it via `pip install uuid7`. -
AttributeError: module 'uuid7_standard' has no attribute 'uuid7'
cause You are likely trying to call a function or attribute (like `uuid7.uuid7()` or `uuid7.generate()`) that exists in the *other* `uuid7` package, but not in `uuid7_standard`.fixUse the correct API for `uuid7-standard`. The main generation function is `generate_uuid()` (e.g., `from uuid7_standard import generate_uuid; new_uuid = generate_uuid()`). -
ValueError: bad badly formed hexadecimal UUID string
cause This error occurs when attempting to create a `UUID7` object from a string that is not a valid UUID format (e.g., incorrect length, invalid characters, or missing hyphens).fixEnsure the input string conforms to the UUID standard, which is a 32-character hexadecimal string, optionally separated by hyphens (e.g., `UUID7('018d9f0a-b2c3-7a4e-9f0d-1e2f3g4h5i6j')`).
Warnings
- gotcha There is a significant potential for confusion with the `uuid7` package on PyPI. The `uuid7` package implements an older draft of the UUIDv7 standard, which is different from the final standard implemented by `uuid7-standard`.
- gotcha The `generate_uuid()` function returns a `UUID7` object, which provides methods for accessing its components (like `datetime`). It does not directly return a string. If a string representation is immediately needed, explicit conversion is required.
Install
-
pip install uuid7-standard
Imports
- generate_uuid
import uuid7
from uuid7_standard import generate_uuid
- UUID7
from uuid7 import UUID7
from uuid7_standard import UUID7
Quickstart
from uuid7_standard import generate_uuid, UUID7
# Generate a UUIDv7 object
new_uuid = generate_uuid()
print(f"Generated UUIDv7 object: {new_uuid}")
print(f"Type: {type(new_uuid)}")
print(f"UUIDv7 as string: {str(new_uuid)}")
# You can also parse a UUIDv7 string into a UUID7 object
# Example valid UUIDv7 string (replace with your actual UUID if needed)
uuid_str = str(new_uuid) # Or use a known valid UUIDv7 string
try:
parsed_uuid = UUID7(uuid_str)
print(f"Parsed UUIDv7 object: {parsed_uuid}")
print(f"Timestamp of parsed UUID: {parsed_uuid.datetime}")
except ValueError as e:
print(f"Error parsing UUID: {e}")