Unable to import torchtext (from torchtext.datasets import IMDB from torchtext.vocab import vocab)

I am running code on an Arch Linux Machine. First I installed torchtext from Arch User Repository (AUR), and when I import it, it gives me the error.

segmenation fault (core dumped)

I also created a virtual environment and installed torchtext with pip. But was not able to make it work.

In [1]: import torchtext
/home/theo/test/env/lib/python3.12/site-packages/torchtext/__init__.py:7: SyntaxWarning: invalid escape sequence '\ '
  "\n/!\ IMPORTANT WARNING ABOUT TORCHTEXT STATUS /!\ \n"
---------------------------------------------------------------------------
OSError                                   Traceback (most recent call last)
Cell In[1], line 1
----> 1 import torchtext

File ~/test/env/lib/python3.12/site-packages/torchtext/__init__.py:18
     15     _WARN = False
     17 # the following import has to happen first in order to load the torchtext C++ library
---> 18 from torchtext import _extension  # noqa: F401
     20 _TEXT_BUCKET = "https://download.pytorch.org/models/text/"
     22 _CACHE_DIR = os.path.expanduser(os.path.join(_get_torch_home(), "text"))

File ~/test/env/lib/python3.12/site-packages/torchtext/_extension.py:64
     59     # This import is for initializing the methods registered via PyBind11
     60     # This has to happen after the base library is loaded
     61     from torchtext import _torchtext  # noqa
---> 64 _init_extension()

File ~/test/env/lib/python3.12/site-packages/torchtext/_extension.py:58, in _init_extension()
     55 if not _mod_utils.is_module_available("torchtext._torchtext"):
     56     raise ImportError("torchtext C++ Extension is not found.")
---> 58 _load_lib("libtorchtext")
     59 # This import is for initializing the methods registered via PyBind11
     60 # This has to happen after the base library is loaded
     61 from torchtext import _torchtext

File ~/test/env/lib/python3.12/site-packages/torchtext/_extension.py:50, in _load_lib(lib)
     48 if not path.exists():
     49     return False
---> 50 torch.ops.load_library(path)
     51 return True

File ~/test/env/lib/python3.12/site-packages/torch/_ops.py:1295, in _Ops.load_library(self, path)
   1290 path = _utils_internal.resolve_library_path(path)
   1291 with dl_open_guard():
   1292     # Import the shared library into the process, thus running its
   1293     # static (global) initialization code in order to register custom
   1294     # operators with the JIT.
-> 1295     ctypes.CDLL(path)
   1296 self.loaded_libraries.add(path)

File /usr/lib/python3.12/ctypes/__init__.py:379, in CDLL.__init__(self, name, mode, handle, use_errno, use_last_error, winmode)
    376 self._FuncPtr = _FuncPtr
    378 if handle is None:
--> 379     self._handle = _dlopen(self._name, mode)
    380 else:
    381     self._handle = handle


Any help will be appreciated.

Version mismatch problem; in the conda environment, do the following:
pip uninstall torch
pip install torch=2.3.0
pip install torchtext=0.18

5 Likes

Thank you for pointing the version mismatch out. Just a small fix in terms syntax (at least in my case):

pip install torch==2.3.0
pip install torchtext==0.18

with two equal signs == instead of single =.

1 Like

Thanks for correcting the typo I made.

Thank you for resolving the problem.