How to import and use python Levenshtein extension on OSX?

LA_ picture LA_ · Jan 27, 2015 · Viewed 42.1k times · Source

I've downloaded the python-Levenshtein archive and extracted Levenshtein dir. So, in result I have the following files structure:

Levenshtein
  - __init__.py
  - _levenshtein.c
  - _levenshtein.h
  - StringMatcher.py
myscript.py

And the following myscript.py content:

from Levenshtein import *
from warnings import warn

print Levenshtein.distance(string1, string2)

But I get the following error -

Traceback (most recent call last):
  File "myscript.py", line 1, in <module>
    from Levenshtein import *
  File "/path/to/myscript/Levenshtein/__init__.py", line 1, in <module>
    from Levenshtein import _levenshtein
ImportError: cannot import name _levenshtein

What is wrong here?

Answer

Franck Dernoncourt picture Franck Dernoncourt · Apr 20, 2015

To install the python-Levenshtein package:

  • pip install python-levenshtein

(This requires pip, but most modern Python installations include it.)