【问题标题】:Nltk is installed but nltk_utils returns ModuleNotFoundErrorNltk 已安装但 nltk_utils 返回 ModuleNotFoundError
【发布时间】:2022-12-18 18:49:04
【问题描述】:

我正在使用虚拟环境,当我尝试导入时,我已经用 pip3 安装了 nltk 模块nltk_utils我收到 ModuleNotFoundError

>>> import nltk
>>> import nltk_utils
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'nltk_utils'

我也试过没有 virtualenv 但没有运气

操作系统:Ubuntu

蟒蛇版本:3.9.5

海湾合作委员会:10.3.0

【问题讨论】:

    标签: python pip nltk modulenotfounderror


    【解决方案1】:

    nltk_utils 不是 nltk 附带的任何东西。你是指nltk.util,也就是described here吗?

    否则 nltk_utils 在一些使用 nltk 的示例中使用,其中它是一个自定义文件,其中包含与 nltk 交互的有用函数(例如在 this chatbot example 中)所以如果您正在学习一些教程或类似内容,请检查他们是否提到nltk_utils 应该包含的地方

    【讨论】:

      【解决方案2】:

      添加到用户 FlyingTeller 的答案中:
      我来到这里遇到了同样的问题,我关注了exact same tutorial由用户 FlyingTeller 链接。引用的导入“nltk_utils”是在教程范围内制作的自定义文件。

      解决问题:
      您可以在教程创建者的 github 上找到“nltk_utils”: https://github.com/patrickloeber/pytorch-chatbot/blob/master/nltk_utils.py (有关该文件的更多说明,请查看教程中链接的视频)。

      更新:您还需要文件“model.py”,它也可以在上面链接的 github 中找到。

      在那之后,你可能仍然会遇到错误,在我的例子中,我需要将“#train model”部分移到主要的并将标签转换为 int。调整后的代码如下所示:

      ...
      if __name__ == '__main__':
          # Train the model
          for epoch in range(num_epochs):
              for (words, labels) in train_loader:
                  words = words.to(device)
                  labels = labels.type(torch.LongTensor) # <- Fix from here: https://*.com/a/71149364/18456868
                  labels = labels.to(device)
      
                  # Forward pass
                  outputs = model(words)
      ...
      

      在那之后,我开始工作了: Output of script after about 3 minutes of training

      【讨论】: