脚本使用IDLE而不是PowerShell

时间:2014-06-17 15:06:12

标签: python nltk attributeerror

我在这段代码中遇到了这个问题:

import nltk

sentence = "Hello everyone on stackoverflow!"
tokens = nltk.word_tokenize(sentence)

print tokens

从PowerShell中运行时抛出这种错误:

    Traceback (most recent call last):
      File "tokenize.py", line 1, in <module>
        import nltk
      File "C:\PYTHON27\lib\site-packages\nltk\__init__.py", line 105, in <module>
        from collocations import *
      File "C:\PYTHON27\lib\site-packages\nltk\collocations.py", line 37, in <module>
        from nltk.util import ingrams
      File "C:\PYTHON27\lib\site-packages\nltk\util.py", line 12, in <module>
        import pydoc
      File "C:\PYTHON27\LIB\pydoc.py", line 55, in <module>
        import sys, imp, os, re, types, inspect, __builtin__, pkgutil, warnings
      File "C:\PYTHON27\LIB\inspect.py", line 39, in <module>
        import tokenize
      File "C:\stuff\tokenize.py", line 4, in <module>
        tokens = nltk.word_tokenize(sentence)
      AttributeError: 'module' object has no attribute 'word_tokenize'

但是相同的代码在IDLE中没有问题:

>>> import nltk
>>> sentence = "Hello everyone on stackoverflow!"
>>> tokens = nltk.word_tokenize(sentence)
>>> print tokens
['Hello', 'everyone', 'on', 'stackoverflow', '!']
>>> 

无论我做什么,我似乎都无法解决问题。起初,我认为它是目录中的“”空间,所以我将其更改为“stuff”,重新安装nltk但问题仍然存在。

我很感激一些帮助:)

1 个答案:

答案 0 :(得分:2)

如果我正确读取了堆栈跟踪,则调用脚本tokenize.py。但是tokenize已经是标准Python库的一个模块。并且仍然相应于堆栈跟踪,tokenize模块间接从nltk调用。但是当你在当前目录中有一个tokenize.py时,Python取而代之的是标准的。{/ p>

您应该将脚本重命名为与标准Python库不冲突的名称。