【问题标题】:Getting ImportError when using torchtext使用 torchtext 时出现 ImportError
【发布时间】:2021-06-07 12:21:20
【问题描述】:

当我尝试运行这行代码时出现以下错误:

from torchtext.data import Field, TabularDataset, BucketIterator, Iterator


ImportError: cannot import name 'Field' from 'torchtext.data' (C:\Users\user1\anaconda3\lib\site-packages\torchtext\data\__init__.py)

我想知道是否有人知道问题可能是什么以及如何解决?

【问题讨论】:

  • 你能说出你使用的是哪个版本的torchtext吗?
  • 抱歉我的torchtext版本是0.9.0
  • 我可以使用 from torchtext.legacy import data 代替我当前的导入语句吗?
  • 您也可以使用旧版,或者降级到 torchtext==0.8.0
  • 好的,我先试试 legacy,然后再尝试降级 torchtext,看看我的结果有什么不同。你想让你的评论成为答案吗?

标签: python importerror torch


【解决方案1】:

torchtext.data 变为 torchtext.legacy.data

from torchtext.legacy.data import Field, TabularDataset, BucketIterator, Iterator

【讨论】:

  • 安装torchtext,pip install -U torchtext
【解决方案2】:
conda install -c pytorch torchtext==0.8

这在 Ubuntu 18.04 上对我有用。

【讨论】:

    【解决方案3】:

    我在使用 colab 时想到了这个,然后我更新了 torchtext 版本

    pip install -U torchtext
    

    它对我有用

    【讨论】:

      猜你喜欢
      • 2022-01-03
      • 2021-04-11
      • 1970-01-01
      • 2018-03-09
      • 2017-05-11
      • 2021-06-26
      • 1970-01-01
      • 1970-01-01
      • 2018-11-06
      相关资源
      最近更新 更多