Registry / serialization / levenshtein

levenshtein

JSON →
library0.27.3pypypi✓ verified 52d ago

The `levenshtein` Python C extension module provides highly optimized functions for fast computation of Levenshtein (edit) distance, string similarity, and other related metrics. It is currently at version 0.27.3 and maintains an active release cadence with regular updates to support newer Python versions.

serializationdata
pip install levenshtein
Install & Compatibility
Where this runs
tested against v0.27.3 · 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
musl
py 3.103.950 runs
installs and imports cleanly · install 0.0s · import 0.038s · 37.8MB
glibc
py 3.103.950 runs
installs and imports cleanly · install 2.6s · import 0.033s · 31MB
32MB installed
● package 32MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Levenshtein
import Levenshtein
from Levenshtein import distance
The primary functions like `distance`, `ratio`, `editops` are direct attributes of the imported `Levenshtein` module.

This quickstart demonstrates how to calculate the Levenshtein distance, the normalized similarity ratio, and retrieve the sequence of edit operations between two strings using the `levenshtein` library.

import Levenshtein string1 = "kitten" string2 = "sitting" # Calculate Levenshtein distance distance = Levenshtein.distance(string1, string2) print(f"Levenshtein distance between '{string1}' and '{string2}': {distance}") # Calculate Levenshtein ratio (normalized similarity) ratio = Levenshtein.ratio(string1, string2) print(f"Levenshtein ratio between '{string1}' and '{string2}': {ratio:.2f}") # Get edit operations edit_ops = Levenshtein.editops(string1, string2) print(f"Edit operations: {edit_ops}")
Debug
Known issues
breakingThe official package name on PyPI was changed from `python-Levenshtein` to `levenshtein`. While `python-Levenshtein` still exists and depends on the `levenshtein` package, using the new name directly is recommended for clarity and to ensure future compatibility.
fix
Update your `requirements.txt` and `pip install` commands to use `levenshtein`. If you were importing as `from Levenshtein import ...`, this import path remains correct for both package names.
affects: <=0.12.2 (for direct `python-Levenshtein` usage), all versions (for best practice)
breakingSupport for older Python versions is periodically dropped. For instance, Python 3.8 support was removed in version 0.26.0, and Python 3.9 support was removed in version 0.27.2.
fix
Ensure your Python environment meets the minimum requirement, which is currently Python 3.10 or later for version 0.27.3.
affects: 0.26.0+ (for Python 3.8), 0.27.2+ (for Python 3.9)
gotchaThe `Levenshtein.ratio()` function calculates a normalized 'Indel similarity' where substitutions are implicitly treated as a deletion followed by an insertion (costing 2 edit operations), rather than a single substitution (costing 1). This can lead to unexpected ratio values if users anticipate a different weighting scheme for substitutions.
fix
Be aware of the underlying calculation for `ratio()`. If a different similarity metric or weighting is required, consider using `Levenshtein.distance()` with custom `weights` parameters (e.g., `weights=(1,1,1)` for equal costs) and calculating your own normalized ratio, or explore other libraries like `rapidfuzz` that offer various ratio algorithms.
affects: All versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'Levenshtein'
The `levenshtein` library, version 0.27.3 and newer, is imported using `import levenshtein` (lowercase 'l'). This error occurs when attempting to import it with `import Levenshtein` (uppercase 'L'), which was the convention for the older `python-Levenshtein` package, or when the `levenshtein` package itself is not installed in the active environment.
fix
Ensure the package is installed using `pip install levenshtein` and then import it as `import levenshtein` in your Python code. If you specifically need the old `python-Levenshtein` package, install it with `pip install python-Levenshtein` and then `import Levenshtein`.
ERROR: Command errored out with exit status 1: ... error: Microsoft Visual C++ 14.0 or greater is required. (or fatal error: Python.h: No such file or directory)
The `levenshtein` package is a C extension and requires a C/C++ compiler and Python development headers to be present on the system for successful compilation during installation.
fix
On Windows, install 'Build Tools for Visual Studio' (e.g., Visual Studio 2022 Build Tools) with the 'Desktop development with C++' workload. On Debian/Ubuntu, run `sudo apt-get install build-essential python3-dev`. On Fedora/RHEL, use `sudo dnf install @development-tools python3-devel`. On macOS, install Xcode Command Line Tools with `xcode-select --install`.
NameError: name 'distance' is not defined
This error occurs when the `distance` function is called without its module prefix, typically after using `import levenshtein` instead of `from levenshtein import distance`.
fix
Either call the function using the module prefix, like `levenshtein.distance(str1, str2)`, or import the function directly using `from levenshtein import distance`.
TypeError: distance expected two Strings or two Unicodes
The `levenshtein.distance()` function was invoked with arguments that are not Python string (str) or Unicode objects, such as lists, Pandas Series, or None values.
fix
Ensure that both arguments passed to `levenshtein.distance()` are valid string (str) objects. If processing a collection of strings, iterate over them and pass individual string elements to the function.
Upgrade
Version history
0.27.3latest on PyPI
Audit
Dependencies

No dependency data recorded yet.

Agent activity
17 hits · last 30 days
node
6
seranking-bot
4
ahrefsbot
2
Amazon
1
Resources