【问题标题】:Pycharm doesn't recognize nltk (installed with Anaconda)Pycharm 无法识别 nltk(随 Anaconda 安装)
【发布时间】:2016-12-17 19:50:24
【问题描述】:

我正在使用 PyCharm 编写一个使用 nltk 包的程序。我的第一行是:

 from nltk import word_tokenize, sent_tokenize

我在 PyCharm 的 2.7 Python 环境(我正在使用的环境)中导入了 nltk 包,如下所示:

但是,PyCharm 无法识别 from nltk.. 行。它是灰色的;它也显示了这个错误:

This inspection detects names that should resolve but don't. Due to dynamic dispatch and duck typing, this is possible in a limited but useful number of cases. Top-level and class-level items are supported better than instance items.

这是我的代码:

from nltk import word_tokenize, sent_tokenize

annot1 = [(500L, u'[they seldom desire anything unless it belongs to     others]')]
annot2 = (500L, u'[they seldom desire anything unless it belongs to others]')

def scope_match(annot1, annot2):
    tokens1 = annot2[1].encode('utf-8')
    print type(tokens1)
    for string in tokens1:
        tokens2 = nltk.word_tokenize(string)
        print 'these are the tokens: ', tokens2
        new2 = [a.strip('[]').encode('utf-8') for a in tokens2]
        print new2

scope_agr = scope_match(annot1, annot2)
print scope_agr

当我运行代码时,我得到了这个错误: `C:\Users\nepal\Anaconda3\envs\py27\python.exe /Users/nepal/PycharmProjects/ScopeCue/ScopeComparison/scope-compare-inter-annotation-agreement-TEST.py

Traceback (most recent call last):
  File "C:/Users/nepal/PycharmProjects/ScopeCue/ScopeComparison/scope-compare- inter-annotation-agreement-TEST.py", line 1, in <module>
from nltk import word_tokenize, sent_tokenize
ImportError: cannot import name word_tokenize

Process finished with exit code 1`

有人可以指导我解决这个问题吗?提前非常感谢。

【问题讨论】:

  • 通常该错误意味着存在有条件定义的变量,因此如果导入语句显示为灰色,则 PyCharm 认为这是一条注释,然后给您一个错误,因为 word_tokenize 未定义跨度>
  • 文件中是否可能有奇怪的非打印字符?您能否在此处发布代码 - 然后从 SO 复制回 PyCharm 并查看它是否有效? (有时有效 - 摆脱奇怪的字符)
  • 我现在添加了代码。非常感谢您的帮助!
  • (请同时包含完整的回溯)它对我有用......我得到了错误nltk is undefined,因为您直接导入word_tokenize,然后尝试使用nltk.word_tokenize,但导入部分工作。
  • 我现在添加了完整的错误代码。很抱歉,这看起来很奇怪!

标签: python pycharm nltk conda


【解决方案1】:

您的导入错误显示模块nltk 已找到,但不包含word_tokenize。 99% 的情况下,这意味着您在脚本所在的目录中创建了一个文件 nltk.py

事实上,您似乎是其中的一个例外——类似于:您在 cmets 中发布的最后一个错误跟踪表明您已经创建了一个完整的 nltk 包(带有 __init__.py 的文件夹)!去掉它或者重命名它,以便python可以找到真正的nltk

【讨论】:

  • 我有一个带有__init__.py 的文件,我把它移走了。我没有名为nltk.py 的文件。不幸的是,PyCharm 仍然无法识别from nltk.tokenize import word_tokenize(它是灰色的,我得到相同的This inspection detects names that should resolve but don't.... 等。知道会发生什么吗?..
  • 您移动了文件夹nltk?你确定吗?只是忘记pycharm。进入一个交互式 python 会话,做一个简单的import nltk,然后像@Tadhg 告诉你的那样检查nltk.__file__。 (如果您不知道如何运行交互式会话,或者在脚本中执行此操作。)
  • 这是我得到的:'C:\\Users\\nepal\\Anaconda3\\envs\\py27\\lib\\site-packages\\nltk\\__init__.pyc' 你知道出了什么问题吗?....(关于删除 __init__.py 文件的注意事项:我删除的是包含该文件的子文件夹)
  • 既然有atom (atom.io) 或sublime (sublimetext.com),为什么还要使用pycharm? ;P
【解决方案2】:

我通过从终端使用nltk.download() 下载完整的 nltk 包解决了这个问题。

所以,我打开了一个新的 python 会话,然后做了:

import nltk

nltk.download()

一个新窗口打开并询问我是否要下载,我接受了。现在它运行良好。

我想知道 Anaconda 是否没有安装完整的 nltk 包?.... 在尝试此解决方案之前,我使用 Anaconda 重新安装了两次(conda install -c anaconda nltk=3.2.1)。但似乎使用该命令并没有得到整个 nltk 包...

无论如何,我希望它可以帮助下一个人。

【讨论】:

  • 您对“nltk 包”(Python 代码)和通过下载器获取的 nltk 数据之间的区别感到困惑。 Anaconda 不会为您下载该文件。几乎可以肯定,下载“书”包对于您想尝试的任何事情都足够了——您不需要“全部”。但是没有一个导入因为缺少数据文件而失败。 调用 sent_tokenize() 会在没有必要数据的情况下给你一个错误。
  • 如果您通过数据引用我想要标记的实际文本,答案是否定的。我没有使用 nltk 包中的任何数据。我正在使用我自己的数据,以我的代码中显示的两个变量annot1annot2 的形式。无论如何,我会问我的顾问。感谢您竭尽全力并回复我。
  • 我指的不是这个,nltk数据不仅仅是语料库。如果您仍然不明白,请在 nltk 网站上花费几分钟,或阅读 nltk.download 上的帮助。
猜你喜欢
  • 2015-01-06
  • 1970-01-01
  • 2020-01-07
  • 2015-09-22
  • 2021-11-19
  • 1970-01-01
  • 2017-05-16
相关资源
最近更新 更多