secrets module

The standard library module for generating random values that are safe to use as passwords, tokens and other secrets.

In more detail

It draws from the operating system's cryptographically secure random source, so its output cannot be predicted. Use secrets.token_hex or secrets.choice wherever the random module would be tempting for anything secret.

Example

import secrets
secrets.token_hex(16)

Related terms

Taught in

References