Install & Compatibility
Where this runs
tested against v1.5.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslpy 3.10–3.910 runs
installs and imports cleanly · install 0.0s · import 0.531s · 24.2MB
glibcpy 3.10–3.910 runs
installs and imports cleanly · install 2.4s · import 0.498s · 25MB
23MB installed
● package 23MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
SecretsCollection
✓ from detect_secrets import SecretsCollection
✗ from detect_secrets.core.usage import run_as_library
run_as_library
✓ from detect_secrets.core.usage import run_as_library
constants
✓ from detect_secrets import constants
This quickstart demonstrates how to programmatically scan a temporary directory containing a file with simulated secrets using `detect-secrets`. It initializes a temporary directory, creates a file with some fake credentials, and then uses `run_as_library` to perform the scan and print the detected secret types.
import os
import tempfile
from pathlib import Path
from detect_secrets.core.usage import run_as_library
def run_detect_secrets_scan():
with tempfile.TemporaryDirectory() as tmpdir:
repo_path = Path(tmpdir)
# Create a dummy file with a fake secret
(repo_path / "my_project").mkdir()
(repo_path / "my_project" / "config.py").write_text(
"API_KEY = 'AKIAIOSFODNN7EXAMPLE' # This is a fake AWS key, DO NOT USE
DB_PASSWORD = 'supersecretpassword123'
SECRET_PHRASE = 'NotARealSecret'
")
print(f"Scanning directory: {repo_path}")
# Run the scan
# 'plugins_used': None lets detect-secrets use its default plugin set.
# 'secret_type_mapping': None uses default mappings.
# 'mount_paths': Specify the path to scan.
scan_results = run_as_library(
plugins_used=None,
secret_type_mapping=None,
mount_paths=[str(repo_path)]
)
# Process results
if scan_results.data:
print("\n--- Detected Secrets ---")
for filepath, secrets in scan_results.data.items():
print(f"File: {filepath}")
for secret in secrets:
print(f" - Type: {secret.type}, Hashed Secret: {secret.hashed_secret}")
else:
print("\nNo secrets detected.")
if __name__ == '__main__':
run_detect_secrets_scan()
detect-secrets --version
Upgrade
Version history
1.5.0latest on PyPI · released May 6, 2024
Audit
Dependencies
scikit-learnoptionalRequired for the optional ML-based gibberish-detector feature. Installed via the `[ml]` extra.