Security and Identifier Modules

secrets

Generates cryptographically suitable random values using the operating system random source. Use it for unpredictable tokens and choices instead of the simulation-oriented random module. Specify token size explicitly, and do not print or log real secrets. Examples show only structural properties, not generated secret values.

Formal Syntax & Structure

python syntaxpython
import secrets

Return Value

module

Module namespace available through import; import is a statement, not a value-returning expression.

Working Example

Example Snippetpython
import secretstoken = secrets.token_hex(16)print(len(token))
Python Reference