How to import and use python Levenshtein extension on OSX?

I downloaded the python-Levenshtein archive and extracted the Levenshtein directory. So, as a result, I have the following file 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?

+7
python osx-yosemite
source share
3 answers

Two other ways to install the python-Levenshtein package :

  • easy_install python-Levenshtein ( setuptools required)
  • pip install python-levenshtein ( pip required)
+16
source share

It seems to me that you have not created the Levenshtein package. Go to the unextracted directory of the source you downloaded (e.g. python-Levenshtein-0.12.0/ ) and build with:

 python setup.py build 

If everything went well (separately, perhaps from some warnings), install on site-packages with

 sudo python setup.py install 

Then I find that I can use the package. e.g. from inside IPython:

 In [1]: import Levenshtein In [2]: string1 = 'dsfjksdjs' In [3]: string2 = 'dsfiksjsd' In [4]: print Levenshtein.distance(string1, string2) 3 

(Note that with your (possibly unreasonable) wildcard, you should just use distance(string1, string2) without a prefix with the package name).

+9
source share

You can try setting environment variables:

add the directory paths python-Levenshtein-master\build\lib.win-amd64-2.7\Levenshtein and python-Levenshtein-master\build\temp.win-amd64-2.7\Release\Levenshtein to your system PATH environment variable.

-one
source share

All Articles