18

I have PyTorch installed in my machine but whenever I try to do the following-

from torchtext import data
from torchtext import datasets

I get the following error.

ImportError: No module named 'torchtext'

How can I install torchtext?

kmario23
  • 57,311
  • 13
  • 161
  • 150
Wasi Ahmad
  • 35,739
  • 32
  • 114
  • 161

8 Answers8

19

As stated in the PyTorch forum, simply do:

$ pip install https://github.com/pytorch/text/archive/master.zip

That should do it.


If not, as an alternative, try:

$ pip install torchtext

For more advanced options, dependencies, and customized installations see:

pypi.org/project/torchtext

kmario23
  • 57,311
  • 13
  • 161
  • 150
9

I am using conda and succeeded with

conda install -c pytorch torchtext
Sheldon Wei
  • 1,198
  • 16
  • 31
  • Reference: [Skusu](https://stackoverflow.com/users/5407040/skusku) suggested this in their comment on [this answer](https://stackoverflow.com/a/55081539/623735) in 2019. This is what I do as well. – hobs Oct 13 '20 at 01:29
  • Worked for me in November, 2021 – StatguyUser Nov 03 '21 at 01:23
8

Try

pip install torchtext

For more details on installation please read pytorch github

Dharman
  • 30,962
  • 25
  • 85
  • 135
Abhishek K
  • 645
  • 1
  • 11
  • 23
7

The package was released with setuptools support. You can clone the repository and run python setup.py install. Unfortunately, I don't think that they have released it on pip.

Alessandro Suglia
  • 1,907
  • 1
  • 16
  • 23
2

I got the import statements to work after i ran these commands:

conda create --name test5 python=3.6
conda install -c pytorch pytorch torchvision cpuonly torchtext
python
>>> from torchtext import data
>>> from torchtext import datasets
Donald S
  • 1,583
  • 1
  • 10
  • 26
0

Or if you are using conda, you can run

    conda install -c derickl torchtext

as per https://anaconda.org/derickl/torchtext

CrnlWes
  • 393
  • 3
  • 6
  • 5
    This did not work for me. Going to anaconda.org and searching for torchtext revealed: https://anaconda.org/pytorch/torchtext which states that one should use: conda install -c pytorch torchtext – Skusku Nov 13 '19 at 20:27
0
pip install --upgrade git+https://github.com/pytorch/text

I used it to ugrade on google colab

vikanksh nath
  • 359
  • 3
  • 5
0

Try this command it fixed the problem for me: pip install torchtext==0.4