Python nltk.tokenize从Shell正确执行,但作为脚本文件出现错误

Python nltk.tokenize从Shell正确执行,但作为脚本文件出现错误,python,nltk,Python,Nltk,我在mac os 10.11.4中使用python idle 3.5.1 我直接在pythonshell中执行以下代码,它工作得非常好 >>> import nltk >>> from nltk.tokenize import sent_tokenize, word_tokenize >>> sample_sentence = "Hi, this is a sample sentence. Python is great" >>&

我在mac os 10.11.4中使用python idle 3.5.1 我直接在pythonshell中执行以下代码,它工作得非常好

>>> import nltk
>>> from nltk.tokenize import sent_tokenize, word_tokenize
>>> sample_sentence = "Hi, this is a sample sentence. Python is great"
>>> sample_sentence
'Hi, this is a sample sentence. Python is great'
>>> sent_tokenize(sample_sentence)
['Hi, this is a sample sentence.', 'Python is great']
>>> 
但我保存的代码与脚本文件相同,并且在执行时 我犯了一个错误

import nltk
from nltk.tokenize import sent_tokenize, word_tokenize
sample_sentence = "Hi, this is a sample sentence. Python is great"
print(sent_tokenize(sample_sentence))
当我运行这个脚本文件时,我得到了以下错误

重新启动:/Users/Documents/sample\u code/python3\u sample.py回溯 (最近一次调用上次):文件 “/Users/Documents/sample\u code/python3\u sample.py”,第2行,在 从nltk.tokenize导入发送\u tokenize,单词\u tokenize导入错误:否 名为“nltk.tokenize”的模块“nltk”不是一个包

我不明白。请帮忙


谢谢。

如果您将python脚本命名为
nltk.py
-请重命名脚本。

在我的示例中,文件名不是nltk.py,而是python3\u sample.py。但我把它改名为通用名。“仍然在经历同样的问题。”Santhoshrinivasan好的,在我删除答案之前。您能检查一下您的项目或python路径中是否有
nltk.py
吗?先生,谢谢您的帮助。我在同一个目录中有一个包含nltk.py和nltk.pyc的旧文件。我把这两个都删除了,效果很好。同样的问题是:和