How to Generate UUIDs in Python

Generate UUIDs in Python with the standard uuid module for v4 and with uuid.uuid7() on Python 3.14 or newer, or a compatibility package on older runtimes.

Recommendation: choose UUID v4 for random identifiers, and UUID v7 when you want sortable IDs and your runtime supports it.

How to generate UUID v4 in Python

import uuid

id = uuid.uuid4()
print(id)

How to generate UUID v7 in Python

Python 3.14 added uuid.uuid7(). On older versions, use a compatibility package that implements RFC 9562.

import uuid

id = uuid.uuid7()
print(id)

Native support notes

  • Python uses UUID naming consistently across the standard library.
  • UUID v4 is standard and portable.
  • UUID v7 is built in on Python 3.14 and later.
  • Keep v7 examples guarded by runtime compatibility notes in shared code or documentation.

Practical notes

  • UUID v4 is still the safest default when you need wide compatibility.
  • UUID v7 is useful when insertion order or log ordering matters.
  • Do not assume every Python environment has the same UUID feature set.

Frequently Asked Questions

Yes, especially when you want simple random IDs that work consistently across almost every Python deployment.

uuid.uuid7() is native starting in Python 3.14. Use a UUID compatibility library or keep using v4 until your deployment target reaches that version.

Learn more

These articles expand on related concepts, formats and practical considerations.

By using this site, you agree to our Privacy Policy and Terms of Service. You are not permitted to use the GUIDs (also known as UUIDs) generated by this site or use any other content, services and information available if you do not agree to these terms.
Disclaimer: All information is provided for general educational and technical reference only. While we aim to keep the content accurate, current and aligned with published standards, no guarantees are made regarding completeness, correctness or suitability for any specific use case.
GUID specifications, best practices, security guidance, database behavior and ecosystem conventions (including cloud platforms and identifier formats) may change over time or differ by implementation. Examples, recommendations and comparisons are illustrative and may not apply universally.
This content should not be considered legal, security, compliance or architectural advice. Before making critical design, security or production decisions, always consult the latest official standards and vendor-specific documentation.
Always evaluate behavior in your own environment.
Standards Compliance: The GUIDs generated by this site conform to RFC 4122 and RFC 9562 specifications whenever possible, using cryptographically secure random number generation.