成功解决 ImportError: cannot import name ‘Field‘ from ‘torchtext.data‘

待解决问题

import datetime
import time
from torchtext.data import Field, Dataset, Example, Iterator
import copy
import torch.nn as nn

当试图导入以上包的时候,出现了报错

     13 import datetime
     14 import time
---> 15 from torchtext.data import Field, Dataset, Example, Iterator
     16 import copy
     17 import torch.nn as nn

ImportError: cannot import name 'Field' from 'torchtext.data' (/usr/local/lib/python3.9/dist-packages/torchtext/data/__init__.py)

初步尝试

根据网上的教程,尝试命令“!pip install torchtext==0.9.0”

Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/
Collecting torchtext==0.9.0
  Downloading torchtext-0.9.0-cp39-cp39-manylinux1_x86_64.whl (7.0 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 7.0/7.0 MB 64.6 MB/s eta 0:00:00
Collecting torch==1.8.0
  Downloading torch-1.8.0-cp39-cp39-manylinux1_x86_64.whl (735.5 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 735.5/735.5 MB 2.1 MB/s eta 0:00:00
Requirement already satisfied: tqdm in /usr/local/lib/python3.9/dist-packages (from torchtext==0.9.0) (4.65.0)
Requirement already satisfied: numpy in /usr/local/lib/python3.9/dist-packages (from torchtext==0.9.0) (1.22.4)
Requirement already satisfied: requests in /usr/local/lib/python3.9/dist-packages (from torchtext==0.9.0) (2.27.1)
Requirement already satisfied: typing-extensions in /usr/local/lib/python3.9/dist-packages (from torch==1.8.0->torchtext==0.9.0) (4.5.0)
Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.9/dist-packages (from requests->torchtext==0.9.0) (3.4)
Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.9/dist-packages (from requests->torchtext==0.9.0) (2022.12.7)
Requirement already satisfied: urllib3<1.27,>=1.21.1 in /usr/local/lib/python3.9/dist-packages (from requests->torchtext==0.9.0) (1.26.15)
Requirement already satisfied: charset-normalizer~=2.0.0 in /usr/local/lib/python3.9/dist-packages (from requests->torchtext==0.9.0) (2.0.12)
Installing collected packages: torch, torchtext
  Attempting uninstall: torch
    Found existing installation: torch 2.0.0+cu118
    Uninstalling torch-2.0.0+cu118:
      Successfully uninstalled torch-2.0.0+cu118
  Attempting uninstall: torchtext
    Found existing installation: torchtext 0.15.1
    Uninstalling torchtext-0.15.1:
      Successfully uninstalled torchtext-0.15.1
ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.
torchvision 0.15.1+cu118 requires torch==2.0.0, but you have torch 1.8.0 which is incompatible.
torchmetrics 0.11.4 requires torch>=1.8.1, but you have torch 1.8.0 which is incompatible.
torchdata 0.6.0 requires torch==2.0.0, but you have torch 1.8.0 which is incompatible.
torchaudio 2.0.1+cu118 requires torch==2.0.0, but you have torch 1.8.0 which is incompatible.
Successfully installed torch-1.8.0 torchtext-0.9.0

此后再次运行from torchtext.data import Field, Dataset, Example, Iterator,依然会出现报错

ImportError: cannot import name 'Field' from 'torchtext.data' (/usr/local/lib/python3.9/dist-packages/torchtext/data/__init__.py)

尝试from torchtext.legacy.data import Field, Dataset, Example, Iterator,依旧报错


ImportError                               Traceback (most recent call last)
 in ()
     13 import datetime
     14 import time
---> 15 from torchtext.legacy.data import Field, Dataset, Example, Iterator
     16 import copy
     17 import torch.nn as nn

3 frames
/usr/local/lib/python3.9/dist-packages/torchtext/legacy/data/dataset.py in 
     11 from torchtext.data.utils import RandomShuffler
     12 from .example import Example
---> 13 from torchtext.utils import download_from_url, unicode_csv_reader
     14 
     15 

ImportError: cannot import name 'unicode_csv_reader' from 'torchtext.utils' (/usr/local/lib/python3.9/dist-packages/torchtext/utils.py)

解决方案

使用命令:!pip install torch==1.8.0 torchtext==0.6.0

重新使用命令:from torchtext.data import Field, Dataset, Example, Iterator

成功

你可能感兴趣的:(自用整理,深度学习,pytorch,python)