Error when importing torchtext.data.datasets_utils

Viewed 20

Im trying to import torchtext but got errors even after trying with different version.

from torch.utils.data import DataLoader
from torch.nn.utils.rnn import pad_sequence
import math
from torch.nn import Transformer
import torch.nn as nn
import torch
from torch import Tensor
from torchtext.vocab import build_vocab_from_iterator
from typing import Iterable, List
from torchtext.data.datasets_utils import _RawTextIterableDataset
from torchtext.data.datasets_utils import _read_text_iterator

I got error as shown below.

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
     13 from typing import Iterable, List
     14 # from torchtext.data.datasets_utils import _RawTextIterableDataset
---> 15 from torchtext.data.datasets_utils import _read_text_iterator
     16 import os
     17 import csv

File c:\Users\anaconda3\envs\torch_env1\lib\site-packages\torchtext\data\datasets_utils.py:7, in <module>
      4 import os
      6 from torch.utils.data import functional_datapipe, IterDataPipe
----> 7 from torch.utils.data.datapipes.utils.common import StreamWrapper
      9 try:
     10     import defusedxml.ElementTree as ET

ImportError: cannot import name 'StreamWrapper' from 'torch.utils.data.datapipes.utils.common'

My torch and torchtext version are as below.

Successfully installed torch-1.12.1 torchtext-0.13.1
1 Answers

I solved the issue by using torch=1.10.2 and torchtext=0.11.2.

pip install torch==1.10.2
pip install torchtext=0.11.2

If you want to use cuda or GPU for the torch, then

pip install torch==1.10.2+cu113 -f https://download.pytorch.org/whl/cu113/torch_stable.html
pip install torchtext==0.11.2
Related