Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在OSX上导入和使用python Levenshtein扩展?_Python_Python 2.7_Osx Yosemite - Fatal编程技术网

如何在OSX上导入和使用python Levenshtein扩展?

如何在OSX上导入和使用python Levenshtein扩展?,python,python-2.7,osx-yosemite,Python,Python 2.7,Osx Yosemite,我已经下载了python Levenshtein归档并提取了Levenshtein目录。因此,我的文件结构如下: Levenshtein - __init__.py - _levenshtein.c - _levenshtein.h - StringMatcher.py myscript.py 以及以下myscript.py内容: from Levenshtein import * from warnings import warn print Levenshtein.dis

我已经下载了python Levenshtein归档并提取了Levenshtein目录。因此,我的文件结构如下:

Levenshtein
  - __init__.py
  - _levenshtein.c
  - _levenshtein.h
  - StringMatcher.py
myscript.py
以及以下
myscript.py
内容:

from Levenshtein import *
from warnings import warn

print Levenshtein.distance(string1, string2)
但我得到了以下错误-

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
回溯(最近一次呼叫最后一次):
文件“myscript.py”,第1行,在
从Levenshtein进口*
文件“/path/to/myscript/Levenshtein/\uuu init\uuuuu.py”,中的第1行
从Levenshtein进口u Levenshtein
ImportError:无法导入名称\u levenshtein

这里怎么了

在我看来,您似乎没有构建Levenshtein包。转到您下载的源代码的未扩展目录(例如,
python-Levenshtein-0.12.0/
)并使用以下内容构建:

python setup.py build
如果一切顺利(可能除了一些警告之外),请使用

sudo python setup.py install
然后我发现我可以使用这个软件包。e、 g.从IPython内部:

In [1]: import Levenshtein
In [2]: string1 = 'dsfjksdjs'
In [3]: string2 = 'dsfiksjsd'
In [4]: print Levenshtein.distance(string1, string2)
3
(请注意,对于通配符导入(可能是不明智的),您应该只使用
距离(string1,string2)
,而不使用程序包名称作为前缀)。

要安装程序包:

  • pip安装python levenshtein

(这需要,但大多数现代Python安装都包括它。)

您可以尝试设置环境变量:


将目录
python-Levenshtein-master\build\lib.win-amd64-2.7\Levenshtein
python-Levenshtein-master\build\temp.win-amd64-2.7\Release\Levenshtein
的路径附加到系统环境变量
PATH
/p>在Windows上安装和使用Levenshtein PIP包,Mac和UNIX

使用sudo安装或以管理员身份运行

pip install python-levenshtein
使用以下命令导入代码:

import Levenshtein as lev
在你的代码中你可以这样使用

lev.distance('Levenshtein', 'Lenvinsten')
哪个会输出

四,