Levenshtein String Distance
raw JSON → 0.27.3 verified Tue May 12 auth: no python install: verified
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.
pip install levenshtein Common errors
error ModuleNotFoundError: No module named 'Levenshtein' ↓
cause 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 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) ↓
cause 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. error NameError: name 'distance' is not defined ↓
cause 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. error TypeError: distance expected two Strings or two Unicodes ↓
cause 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. Warnings
breaking The 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.
breaking Support 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.
gotcha The `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.
Install
pip install python-Levenshtein Install compatibility verified last tested: 2026-05-12
python os / libc variant status wheel install import disk
3.10 alpine (musl) levenshtein wheel - 0.03s 37.7M
3.10 alpine (musl) levenshtein - - 0.03s 37.7M
3.10 alpine (musl) python-Levenshtein wheel - 0.03s 37.8M
3.10 alpine (musl) python-Levenshtein - - 0.03s 37.8M
3.10 slim (glibc) levenshtein wheel 3.0s 0.02s 31M
3.10 slim (glibc) levenshtein - - 0.02s 31M
3.10 slim (glibc) python-Levenshtein wheel 2.9s 0.02s 31M
3.10 slim (glibc) python-Levenshtein - - 0.02s 31M
3.11 alpine (musl) levenshtein wheel - 0.05s 39.6M
3.11 alpine (musl) levenshtein - - 0.05s 39.6M
3.11 alpine (musl) python-Levenshtein wheel - 0.05s 39.7M
3.11 alpine (musl) python-Levenshtein - - 0.05s 39.7M
3.11 slim (glibc) levenshtein wheel 2.4s 0.04s 33M
3.11 slim (glibc) levenshtein - - 0.04s 33M
3.11 slim (glibc) python-Levenshtein wheel 2.3s 0.04s 33M
3.11 slim (glibc) python-Levenshtein - - 0.04s 33M
3.12 alpine (musl) levenshtein wheel - 0.04s 31.5M
3.12 alpine (musl) levenshtein - - 0.04s 31.5M
3.12 alpine (musl) python-Levenshtein wheel - 0.04s 31.6M
3.12 alpine (musl) python-Levenshtein - - 0.04s 31.6M
3.12 slim (glibc) levenshtein wheel 2.0s 0.04s 24M
3.12 slim (glibc) levenshtein - - 0.04s 24M
3.12 slim (glibc) python-Levenshtein wheel 2.0s 0.04s 24M
3.12 slim (glibc) python-Levenshtein - - 0.04s 24M
3.13 alpine (musl) levenshtein wheel - 0.04s 31.2M
3.13 alpine (musl) levenshtein - - 0.04s 31.1M
3.13 alpine (musl) python-Levenshtein wheel - 0.04s 31.3M
3.13 alpine (musl) python-Levenshtein - - 0.04s 31.2M
3.13 slim (glibc) levenshtein wheel 2.2s 0.03s 24M
3.13 slim (glibc) levenshtein - - 0.04s 24M
3.13 slim (glibc) python-Levenshtein wheel 2.1s 0.03s 24M
3.13 slim (glibc) python-Levenshtein - - 0.03s 24M
3.9 alpine (musl) levenshtein wheel - 0.03s 37.5M
3.9 alpine (musl) levenshtein - - 0.03s 37.5M
3.9 alpine (musl) python-Levenshtein wheel - 0.03s 37.6M
3.9 alpine (musl) python-Levenshtein - - 0.03s 37.6M
3.9 slim (glibc) levenshtein wheel 3.4s 0.03s 30M
3.9 slim (glibc) levenshtein - - 0.03s 30M
3.9 slim (glibc) python-Levenshtein wheel 3.5s 0.03s 30M
3.9 slim (glibc) python-Levenshtein - - 0.03s 30M
Imports
- Levenshtein wrong
from Levenshtein import distancecorrectimport Levenshtein
Quickstart last tested: 2026-04-24
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}")