Presidio Anonymizer is a Python-based module designed for anonymizing detected Personally Identifiable Information (PII) entities in text. It offers a range of built-in operators (e.g., replace, mask, redact, hash, encrypt) and supports custom anonymization logic. It also includes deanonymization capabilities for reversible operations like decryption. The library is actively maintained by Microsoft, with frequent releases, and is currently at version 2.2.362.
pip install presidio-anonymizerVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to use `AnonymizerEngine` to anonymize text. It manually defines `RecognizerResult` objects, which would typically be generated by `presidio-analyzer`. It shows custom operators for different entity types.
If referential integrity (same hash for the same value) is required, you must explicitly provide a consistent 'salt' parameter to the 'hash' operator config. Use a secure method to generate and store this salt.
Always import `RecognizerResult` specifically from `presidio_anonymizer.entities` when passing results to `AnonymizerEngine`. You may need to cast or convert `presidio-analyzer`'s `RecognizerResult` objects if you generate them from `AnalyzerEngine` and need to satisfy strict type checking.
If specific country-specific recognizers are required, ensure they are explicitly enabled or configured within your `presidio-analyzer` setup.
Upgrade to version 2.2.362 or later. Ensure thorough testing of anonymization output, especially for texts with repeated PII entities separated by whitespace.
For a complete solution, install `presidio-analyzer` and a spaCy language model using `pip install presidio-analyzer "spacy[en]"` and `python -m spacy download en_core_web_lg`.
Instead of `AnonymizerConfig`, you should typically use `OperatorConfig` or directly define operator parameters within the `anonymize` method. Remove the import for `AnonymizerConfig` and adjust your code to use `OperatorConfig` for configuring anonymization operators, if needed.
Ensure both `presidio-analyzer` and `presidio-anonymizer` are updated to compatible versions, ideally the latest stable releases, by running `pip install --upgrade presidio-analyzer presidio-anonymizer`.
Install the package using pip: `pip install presidio-anonymizer`. If you also use the analyzer, install both: `pip install presidio-analyzer presidio-anonymizer`. Additionally, remember to download a spaCy model if using the default NLP engine: `python -m spacy download en_core_web_lg`.
To prevent this behavior, pass the `merge_whitespace_entities=False` parameter to the `anonymize` method of `AnonymizerEngine`.