torchtext ImportError in colab
Asked Answered
D

5

9

I am trying to run this tutorial in colab.

However, when I try to import a bunch of modules:

import io
import torch
from torchtext.utils import download_from_url
from torchtext.data.utils import get_tokenizer
from torchtext.vocab import build_vocab_from_iterator

It gives me the errors for extract_archive and build_vocab_from_iterator:

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-5-a24e72502dbc> in <module>()
      1 import io
      2 import torch
----> 3 from torchtext.utils import download_from_url, extract_archive
      4 from torchtext.data.utils import get_tokenizer
      5 from torchtext.vocab import build_vocab_from_iterator

ImportError: cannot import name 'extract_archive'


ImportError                               Traceback (most recent call last)
<ipython-input-4-02a401fd241b> in <module>()
      3 from torchtext.utils import download_from_url
      4 from torchtext.data.utils import get_tokenizer
----> 5 from torchtext.vocab import build_vocab_from_iterator
      6 
      7 url = 'https://s3.amazonaws.com/research.metamind.io/wikitext/wikitext-2-v1.zip'

ImportError: cannot import name 'build_vocab_from_iterator'

Please help me with this one.

Denounce answered 5/1, 2021 at 9:3 Comment(2)
What version do you have for torchtext (you can do !pip list | grep torchtext in a new cell)Swiftlet
why does torchtext cause so many issues?Landlubber
N
8

You need to upgrade torchtext first

!pip install -U torchtext==0.8.0

Currently, version 0.8.0 works with torch 1.7.0 (no need to upgrade torch, torchvision)

Update (sep 2021)

Currently, torchtext is already 0.10.0 and you don't need to upgrade anything.

Nunci answered 5/1, 2021 at 12:35 Comment(4)
Note: The Pytorch doc for install doesn't include torchtext yet!Japonica
does this work with torch==1.7.1 or only 1.7.0?Landlubber
why does torchtext cause so many issues?Landlubber
It doesn't work with torch 1.11.0+cu113Moltke
M
3

Update December 2021

!pip install -U torchtext==0.10.0

torchtext.data becomes torchtext.legacy.data

use:

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

credit

Marquise answered 13/12, 2021 at 9:22 Comment(0)
G
0

This might help solve your problem:

conda install -c pytorch torchtext==0.8
Goodspeed answered 26/7, 2021 at 9:53 Comment(2)
does this work with torch==1.7.1 or only 1.7.0?Landlubber
why does torchtext cause so many issues?Landlubber
C
0

You can use:

pip install -U torchtext==0.6.0

if 0.8 version is not available

Cusick answered 3/8, 2021 at 10:6 Comment(1)
why does torchtext cause so many issues?Landlubber
K
0

If some one wants to run from terminal a Linux then You should consider what Python you are using.

I checked these answers and no one worked.

Based on the Python version that I have (Python3.10) I used the following installation and it worked.

pip install -U torchtext==0.15.2
Kapok answered 21/5, 2023 at 18:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.