ulid

raw JSON →
1.1 verified Mon Apr 27 auth: no python

ULID (Universally Unique Lexicographically Sortable Identifier) library for Python. Provides encoding and decoding of ULID strings from timestamps and random components. Current version: 1.1, with infrequent releases.

pip install ulid
error ImportError: cannot import name 'ULID' from 'ulid'
cause Incorrect import: trying to import 'ULID' as a class, but ulid module is function-based.
fix
Use 'import ulid' then call ulid.new() etc.
error AttributeError: module 'ulid' has no attribute 'new'
cause Outdated version or incorrect installation. Also possible if the module name conflicts with another library.
fix
Reinstall with 'pip install ulid==1.1' and ensure no other ulid module in PYTHONPATH.
gotcha The library is not actively maintained (last release 2019). May have unhandled edge cases and compatibility issues with Python 3.10+.
fix Consider alternative libraries like python-ulid or ulid-py for active maintenance.
gotcha ulid.new() returns a string, not a ULID object. No built-in ULID class or object representation.
fix Work directly with the string output from ulid.new().
gotcha ulid.decode() expects a string, but returns a tuple (timestamp_ms, random_bits). The random part is an integer, not bytes or a dedicated object.
fix Handle the integer random bits; convert to hex or bytes if needed.

Generates a new ULID, decodes it, and creates one from a timestamp.

import ulid
import time

ulid_str = ulid.new()
print(f"Generated ULID: {ulid_str}")

# Decode a ULID
timestamp_ms, random_part = ulid.decode(ulid_str)
print(f"Timestamp (ms): {timestamp_ms}, Random: {random_part}")

# Create ULID from specific timestamp (in milliseconds)
ts = int(time.time() * 1000)
ulid_from_ts = ulid.from_timestamp(ts)
print(f"ULID from timestamp: {ulid_from_ts}")