Password Strength
The `password-strength` library (PyPI slug: `password-strength`, current version `0.0.3.post2`) provides tools for password strength assessment and validation. It allows defining a `PasswordPolicy` with various rules (e.g., minimum length, character types, entropy) to test if a password meets specified security requirements. The library also offers `PasswordStats` to get a normalized strength score and detailed analysis of a password. While the PyPI package itself was last updated in 2019, its GitHub repository shows more recent activity, suggesting ongoing relevance.
Common errors
-
ModuleNotFoundError: No module named 'password_strength'
cause The library is not installed or the Python environment is incorrect.fixEnsure the library is installed in your current environment using `pip install password-strength`. If using virtual environments, activate the correct one before running your script. -
TypeError: 'PasswordPolicy' object is not callable
cause Attempting to call a `PasswordPolicy` object like a function instead of using its `test()` method.fixAccess validation methods on the `PasswordPolicy` object. For example, use `policy.test(password)` instead of `policy(password)`. -
AttributeError: 'PasswordStats' object has no attribute 'test'
cause Confusing `PasswordStats` (for analysis) with `PasswordPolicy` (for validation). `PasswordStats` provides statistical properties, not policy testing.fixUse `PasswordPolicy` for testing against defined rules (`policy.test(password)`). Use `PasswordStats` to get raw metrics like entropy or complexity (`PasswordStats(password).strength`).
Warnings
- gotcha The default entropy calculation might be less intuitive for users than a complexity score or explicit policy checks. While `entropy_bits` is a fundamental measure, `complexity` (0.00-0.99) or `strength` (0.00-1.00) are often more digestible for direct user feedback.
- gotcha A password might pass basic length and character type rules but still contain easily guessable repetitions (e.g., 'aaaaaa', '123123'). The library's `Policy` object doesn't inherently check for overly repetitive patterns beyond what the entropy calculation might implicitly catch.
- deprecated Direct manipulation or reliance on `weak_bits`, `medium_bits`, `strong_bits` constants might be less recommended than using the `Policy` object's named rules, as policies provide a clearer, more configurable approach to defining acceptable passwords.
Install
-
pip install password-strength
Imports
- PasswordPolicy
from password_strength import PasswordPolicy
- PasswordStats
from password_strength import PasswordStats
Quickstart
from password_strength import PasswordPolicy, PasswordStats
# Define a password policy
policy = PasswordPolicy.from_names(
length=8,
uppercase=1,
numbers=1,
special=1,
nonletters=1
)
# Test a password against the policy
password = "StrongP@ssw0rd!"
errors = policy.test(password)
if not errors:
print(f"Password '{password}' meets the policy requirements.")
else:
print(f"Password '{password}' failed the following checks: {', '.join(errors)}")
# Get detailed strength statistics
stats = PasswordStats(password)
print(f"\nPassword entropy (bits): {stats.entropy_bits:.2f}")
print(f"Password complexity (0.00-0.99): {stats.complexity:.2f}")
print(f"Password strength (0.00-1.00): {stats.strength:.2f}")