UUID (PyPI Backport for Python 2.x)
This PyPI package (version 1.30) provides a backport of the `uuid` module's functionality for older Python versions, specifically 2.3 through 2.5. For Python 2.6 and newer, the `uuid` module is included in the Python standard library and this PyPI package is generally not needed. It was last released in 2006 and is not actively maintained.
Warnings
- breaking This PyPI package (version 1.30) is a backport specifically for Python versions 2.3-2.5. For Python 2.6 and newer, the `uuid` module is included in the Python standard library. Installing this PyPI package on Python 2.6+ or Python 3.x is unnecessary and can lead to confusion or potential conflicts, as the standard library version should be used instead.
- gotcha The `uuid.uuid1()` function generates a UUID using the computer's network address (MAC address) and the current time. This can compromise privacy if the generated UUIDs are exposed, as they could potentially be traced back to the originating machine.
- gotcha Storing UUIDs as plain strings in databases is inefficient. It consumes more space and can degrade query performance compared to using native UUID data types (e.g., in PostgreSQL) or storing them as 16-byte binary data.
- gotcha This specific PyPI `uuid` package (v1.30) and older standard library versions only support UUID versions 1, 3, 4, and 5. Newer, potentially more optimized or privacy-friendly versions like UUIDv6, UUIDv7, and UUIDv8 were added to the standard library starting with Python 3.14.
- gotcha While UUIDs are designed to be highly unique (e.g., collision probability for UUIDv4 is extremely low), they are not absolutely guaranteed to be unique. In extremely high-volume, distributed systems, theoretical collision possibilities exist.
- gotcha The hardware address retrieved by `uuid.getnode()` (used internally by `uuid.uuid1()`) is tied to the system's network interfaces and can change if network configurations are altered (e.g., switching Wi-Fi, using USB Ethernet adapters).
Install
-
pip install uuid
Imports
- uuid
import uuid
- UUID
from uuid import UUID my_uuid_obj = UUID('a1b2c3d4-e5f6-7890-1234-567890abcdef')
Quickstart
import uuid
# Generate a random UUID (version 4)
new_uuid = uuid.uuid4()
print(f"Generated UUID (version 4): {new_uuid}")
print(f"UUID as hex string: {new_uuid.hex}")
# Generate a time-based UUID (version 1)
# Note: uuid1() may expose the computer's MAC address.
time_uuid = uuid.uuid1()
print(f"Generated UUID (version 1): {time_uuid}")
# Reconstruct a UUID from a string
from uuid import UUID
recovered_uuid = UUID(str(new_uuid))
print(f"Recovered UUID: {recovered_uuid}")
print(f"Are they equal? {new_uuid == recovered_uuid}")