How to import and use python Levenshtein extension on OSX?

淺唱寂寞╮ 提交于 2019-12-04 19:35:30

问题


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?


回答1:


To install the python-Levenshtein package:

  • pip install python-levenshtein

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




回答2:


It seems to me like you did not build the Levenshtein package. Go to the unextracted directory of the source you downloaded (for example, python-Levenshtein-0.12.0/) and build with:

python setup.py build

If all went well (apart, possibly, from some warnings), install to your site-packages with

sudo python setup.py install

Then I find I can use the package. e.g. from within 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 (perhaps unwise) wildcard import you should just be using distance(string1, string2) without prefixing with the package name).




回答3:


Installation and usage of Levenshtein PIP package on Windows, Mac and UNIX

Install with sudo or run as admin

pip install python-levenshtein

Import in your code with:

import Levenshtein as lev

than in your code you can use Levenstein functions like this

lev.distance('Levenshtein', 'Lenvinsten')

which will output

4

.




回答4:


You may try to set environment variables:

append the paths of catalogs 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 environment variable PATH.



来源:https://stackoverflow.com/questions/28172261/how-to-import-and-use-python-levenshtein-extension-on-osx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!