Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a GUID/UUID in Python

How do I create a GUID in Python that is platform independent? I hear there is a method using ActivePython on Windows but it's Windows only because it uses COM. Is there a method using plain Python?

like image 296
Jonathon Watney Avatar asked Feb 10 '09 23:02

Jonathon Watney


People also ask

How do you generate a UUID in Python?

UUID 1 to Generate a unique ID using MAC AddressThe uuid. uuid1() function is used to generate a UUID from the host ID, sequence number, and the current time. It uses the MAC address of a host as a source of uniqueness. The node and clock_seq are optional arguments.

What is a GUID Python?

UUID is a 128-bit number used in computer systems to define entities or information uniquely. UUID stands for Universally Unique Identifier. In software created by Microsoft, UUID is regarded as a Globally Unique Identifier or GUID.

How do you create a unique GUID?

To Generate a GUID in Windows 10 with PowerShell, Type or copy-paste the following command: [guid]::NewGuid() . This will produce a new GUID in the output. Alternatively, you can run the command '{'+[guid]::NewGuid(). ToString()+'}' to get a new GUID in the traditional Registry format.


Video Answer


1 Answers

The uuid module provides immutable UUID objects (the UUID class) and the functions uuid1(), uuid3(), uuid4(), uuid5() for generating version 1, 3, 4, and 5 UUIDs as specified in RFC 4122.

If all you want is a unique ID, you should probably call uuid1() or uuid4(). Note that uuid1() may compromise privacy since it creates a UUID containing the computer’s network address. uuid4() creates a random UUID.

Docs:

  • Python 2
  • Python 3

Examples (for both Python 2 and 3):

>>> import uuid  >>> # make a random UUID >>> uuid.uuid4() UUID('bd65600d-8669-4903-8a14-af88203add38')  >>> # Convert a UUID to a string of hex digits in standard form >>> str(uuid.uuid4()) 'f50ec0b7-f960-400d-91f0-c42a6d44e3d0'  >>> # Convert a UUID to a 32-character hexadecimal string >>> uuid.uuid4().hex '9fe2c4e93f654fdbb24c02b15259716c' 
like image 121
stuartd Avatar answered Sep 20 '22 15:09

stuartd