How to create a GUID/UUID in Python

You can use the uuid module in Python to generate a globally unique identifier (GUID), also known as a universally unique identifier (UUID). Here is an example of how to generate a UUID in Python:

import uuid

# Generate a random UUID
random_uuid = uuid.uuid4()

# Print the UUID
print(random_uuid)

Watch a course Python - The Practical Guide

You can also use the uuid1() function to generate a UUID based on the host ID and current time:

import uuid

# Generate a UUID based on the host ID and current time
time_based_uuid = uuid.uuid1()

# Print the UUID
print(time_based_uuid)

You can also use uuid3() and uuid5() to create UUIDs based on the MD5 and SHA-1 hash respectively, with an additional namespace.

import uuid

# Generate a UUID based on the namespace and name
uuid3_uuid = uuid.uuid3(uuid.NAMESPACE_DNS, "example.com")
uuid5_uuid = uuid.uuid5(uuid.NAMESPACE_DNS, "example.com")

# Print the UUID
print(uuid3_uuid)
print(uuid5_uuid)