Random Password Generator
A dependency-free Python library for generating passwords with configurable lengths, character requirements, and exclusions. Version 3 uses the operating system’s cryptographically secure randomness for character selection, length selection, and shuffling.
Installation
Install the published package from PyPI:
python -m pip install random-password-generator
The examples below describe the version 3 API. To try the code in this repository rather than the published release, run python -m pip install . from a local checkout.
Python compatibility
Version 3 requires Python 3.6 or newer. If you need Python 3.5 compatibility, install version 2.2.0:
python -m pip install random-password-generator==2.2.0
On Python 3.5, version 2.2.0 falls back to a non-cryptographic random number generator. Use version 3 on a supported Python release for security-sensitive passwords.
Changelog
3.0.0
- Python support: Requires Python 3.6 or newer. For Python 3.5, use
2.2.0. - Secure randomness: Character selection, length selection, and shuffling now all use the operating system’s secure random source.
- Easier configuration: Pass settings directly to
PasswordGenerator(...)as keyword arguments. Setting attributes after construction still works. - Stricter validation:
generate()rejects configurations that cannot meet the length or character requirements.shuffle_password()no longer coerces lengths such as"20"or20.0; pass20instead.
Quick start
from password_generator import PasswordGenerator
password = PasswordGenerator().generate()
generate() returns a string. By default, it generates a password of 6–16 characters with at least one uppercase letter, one lowercase letter, one digit, and one special character. For a longer password, set minlen and maxlen to the same value.
Configuration
Pass settings as optional keyword arguments to PasswordGenerator(). Omitted settings keep their defaults, and existing code can still change the attributes after creating an instance.
from password_generator import PasswordGenerator
generator = PasswordGenerator(
minlen=24,
maxlen=24,
minuchars=2,
excludeuchars="IO",
excludenumbers="01",
)
password = generator.generate()
| Setting | Description | Default |
|---|---|---|
minlen |
Minimum password length | 6 |
maxlen |
Maximum password length | 16 |
minuchars |
Minimum number of uppercase letters | 1 |
minlchars |
Minimum number of lowercase letters | 1 |
minnumbers |
Minimum number of digits | 1 |
minschars |
Minimum number of special characters | 1 |
excludeuchars |
Uppercase letters to exclude | "" |
excludelchars |
Lowercase letters to exclude | "" |
excludenumbers |
Digits to exclude | "" |
excludeschars |
Special characters to exclude | "" |
generate() checks these settings when called. Lengths and counts must be non-negative integers (not booleans), and exclusions must be strings. Wrong types raise TypeError. Negative values, minlen greater than maxlen, or minimum counts that add up to more than maxlen raise ValueError.
If exclusions leave a required category empty, generate() raises a ValueError that names the category. Setting a category’s minimum to 0 makes it optional; to omit it entirely, also exclude all its characters.
Other generation methods
Sample from a custom character pool
shuffle_password(password, maxlen) samples from the supplied string with replacement. Characters can repeat, and the result can be longer than the input. maxlen is the exact output length and must be a non-negative integer, not a boolean. A length of 0 returns ""; a positive length requires a non-empty character string.
from password_generator import PasswordGenerator
password = PasswordGenerator().shuffle_password("abc123!@#", 20)
Generate a password without repeated characters
non_duplicate_password(maxlen) draws from the built-in pool of 80 distinct characters. A length of 80 is valid; a longer request raises ValueError.
from password_generator import PasswordGenerator
password = PasswordGenerator().non_duplicate_password(20)
The web app and API are maintained separately in the rpg-web repository.
Contributing
Pull requests are welcome. GitHub Actions runs Ruff and the unit tests on pull requests targeting main. To run the same checks locally from the repository root:
uv run --locked --python 3.11 ruff check
uv run --locked --python 3.11 --no-dev python -B -m unittest -v
License
Metadata
Release files for random-password-generator 3.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| random_password_generator-3.0.0.tar.gz | 6.2 kB | Without provenance | Details for random_password_generator-3.0.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| random_password_generator-3.0.0-py3-none-any.whl | Python 3 | none | any | Details for random_password_generator-3.0.0-py3-none-any.whl |
Total release size: 12.1 kB
Release files / random_password_generator-3.0.0.tar.gz
| Download URL | random_password_generator-3.0.0.tar.gz |
|---|---|
| Size | 6.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
20e11a572f278e83738f0a7397c576405bdb0e50549a0d1b35f865fc655a4cb7
|
|
BLAKE2b-256 checksum How to use checksums |
c1852ffa369442c33bb4b0f02fec42551650801fb3b7d9a16a59e8fefce11508
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.6.3
|
Release files / random_password_generator-3.0.0-py3-none-any.whl
| Download URL | random_password_generator-3.0.0-py3-none-any.whl |
|---|---|
| Size | 5.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
23ed0c673ba861cccda94fd90ca9759448c812025b45beeabf70b50e9aefc4e8
|
|
BLAKE2b-256 checksum How to use checksums |
1501f0f8cd14d32b95aef05eda554d139644df179291688699f338d724b13c51
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.6.3
|
Security for random-password-generator 3.0.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.