如何在 OSX 上导入和使用 python Levenshtein 扩展?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28172261/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to import and use python Levenshtein extension on OSX?
提问by LA_
I've downloaded the python-Levenshtein archive and extracted Levenshtein dir. So, in result I have the following files structure:
我已经下载了 python-Levenshtein 档案并提取了 Levenshtein 目录。所以,结果我有以下文件结构:
Levenshtein
- __init__.py
- _levenshtein.c
- _levenshtein.h
- StringMatcher.py
myscript.py
And the following myscript.py
content:
以及以下myscript.py
内容:
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?
这里有什么问题?
回答by xnx
It seems to me like you did not buildthe Levenshtein package. Go to the unextracted directory of the source you downloaded (for example, python-Levenshtein-0.12.0/
) and build with:
在我看来,您没有构建Levenshtein 包。转到您下载的源代码的未解压目录(例如,python-Levenshtein-0.12.0/
)并使用以下命令构建:
python setup.py build
If all went well (apart, possibly, from some warnings), install to your site-packages
with
如果一切顺利(除了一些警告之外),安装到你site-packages
的
sudo python setup.py install
Then I find I can use the package. e.g. from within IPython:
然后我发现我可以使用这个包。例如在 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).
(请注意,对于您的(可能是不明智的)通配符导入,您应该只使用distance(string1, string2)
不带包名称前缀的方式)。
回答by Franck Dernoncourt
To install the python-Levenshteinpackage:
pip install python-levenshtein
pip install python-levenshtein
(This requires pip, but most modern Python installations include it.)
(这需要pip,但大多数现代 Python 安装都包含它。)
回答by Alex Wang
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
.
附加目录的路径python-Levenshtein-master\build\lib.win-amd64-2.7\Levenshtein
,并python-Levenshtein-master\build\temp.win-amd64-2.7\Release\Levenshtein
在您的系统环境变量PATH
。
回答by Harvey
Installation and usage of Levenshtein PIP package on Windows, Mac and UNIX
Levenshtein PIP 包在 Windows、Mac 和 UNIX 上的安装和使用
Install with sudo or run as admin
使用sudo安装或以管理员身份运行
pip install python-levenshtein
Import in your code with:
使用以下命令导入您的代码:
import Levenshtein as lev
than in your code you can use Levenstein functionslike this
不是在你的代码,你可以使用莱文施泰因功能,这样
lev.distance('Levenshtein', 'Lenvinsten')
which will output
这将输出
4
4
.
.