2017-05-06 112 views
1

我已经使用pip install ngram安装了ngram。当我运行下面的代码在python中导入ngram时出错

from ngram import NGram 
c=NGram.compare('cereal_crop','cereals') 
print c 

我得到的错误ImportError: cannot import name NGram

截图吧:screenshot of console window

附:一个类似的问题之前已经被问到using ngram in python,但是那个得到错误的人没有安装ngram,所以安装ngram工作。在我的情况下,尽管安装了ngram,我仍然收到错误。

回答

1

你的Python脚本被命名为ngram.py,所以它定义了一个名为ngram模块。当Python运行from ngram import NGram时,Python最终会在脚本中查找名为NGram的东西,而不是您已安装的ngram模块。

尝试将脚本的名称更改为其他名称,例如ngram_test.py

0

尝试这样的:

import ngram 
c = ngram.NGram.compare('cereal_crop','cereals') 
print c