【问题标题】:Can't access full python module from file, but can from shell无法从文件访问完整的 python 模块,但可以从 shell
【发布时间】:2014-10-02 05:33:28
【问题描述】:

也许我在这里完全遗漏了一些东西,但是当我从 shell 运行这段代码时,它可以工作:

import nltk
tokens = nltk.word_tokenize("foo bar")

然后返回:

['foo','bar']

但是当我把它放到一个文件中并用 python -u "path/to/file/myfile.py" 执行它时,它会返回

AttributeError: 'module' object has no attribute 'word_tokenize'

我已经尝试过重新安装以及我能想到的一切。如果您需要更多信息,请告诉我。

提前致谢!

【问题讨论】:

  • 你的 python 脚本文件名是什么?

标签: python shell module python-module


【解决方案1】:

您很可能将您的文件称为nltk.py,因此python 尝试从该文件导入,而不是实际的nltk 模块。只需重命名您的 .pyfile。

【讨论】:

  • 非常感谢!我是 python 的菜鸟。
  • 别担心,相信我你不是第一个!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-24
  • 1970-01-01
  • 2018-04-04
  • 2020-04-18
相关资源
最近更新 更多