brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: python -m uuid prints a fresh UUID without writing any code

Python
~/tips/tip-python-m-uuid-generate

python -m uuid

Generates a random UUID, or a name-based one, straight from the command line on Python 3.12 or newer.

I used to open a REPL, import uuid and call uuid4() every time I needed an identifier for a test record. Since Python 3.12 the module has its own command line.

python -m uuid
python -m uuid -u uuid1

The default is a random UUID from uuid4(). The -u option picks another generator, so the second line uses uuid1().

There is a name-based form too. uuid3 and uuid5 hash a name inside a namespace, and the namespace can be a UUID or one of the predefined ones: @dns, @url, @oid or @x500.

python -m uuid -u uuid5 -n @url -N example.com

Run that twice and you get the same UUID twice, because the result only depends on the namespace and the name. That makes it useful for stable IDs derived from something you already have.

Python 3.14 added more: -u also accepts uuid6, uuid7 and uuid8, and -C or --count prints several UUIDs in one go.

The caveat is the version. On Python 3.11 and older there is nothing to run, and python -m uuid simply executes the module file without printing anything useful. Check with python --version first, and use the uuid4() one-liner on older systems.

Further reading: Python docs: uuid, command-line usage

More Python tips import asyncio from enum import StrEnum, auto except* ValueError as eg: import pytest pytest --maxfail=2 All Python tips (107) All topics

Link