Error trying to import function from torchtext

I have setup an Amazon Linux EC2 gpu instance with Python 3.11.6 and am trying to import torch and torch text:

>>> import torch
>>> from torchtext.vocab import build_vocab_from_iterator

This raises the following error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/ec2-user/***/.env/lib64/python3.11/site-packages/torchtext/__init__.py", line 6, in <module>
    from torchtext import _extension  # noqa: F401
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/ec2-user/***/.env/lib64/python3.11/site-packages/torchtext/_extension.py", line 64, in <module>
    _init_extension()
  File "/home/ec2-user/***/.env/lib64/python3.11/site-packages/torchtext/_extension.py", line 58, in _init_extension
    _load_lib("libtorchtext")
  File "/home/ec2-user/***/.env/lib64/python3.11/site-packages/torchtext/_extension.py", line 50, in _load_lib
    torch.ops.load_library(path)
  File "/home/ec2-user/***/.env/lib64/python3.11/site-packages/torch/_ops.py", line 1350, in load_library
    ctypes.CDLL(path)
  File "/usr/lib64/python3.11/ctypes/__init__.py", line 376, in __init__
    self._handle = _dlopen(self._name, mode)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^
OSError: /home/ec2-user/***/.env/lib/python3.11/site-packages/torchtext/lib/libtorchtext.so: undefined symbol: _ZN5torch3jit17parseSchemaOrNameERKSs

I’ve seen other people have run into this issue when they have multiple versions of torch installed, but this instance doesn’t have anything else installed, so I’m at a loss. Any ideas?

Could you share the PyTorch and torchtext versions you are using?

Trying to use torch 2.6.0 (2.5.1 gave the error as well) and torchtext 0.18.0.

Just to be complete, it looks like I’m getting the same error importing any function or class (like Vocab) from torchtext, or even torchtext itself.

Thank you for the clarification!
I’m afraid this regression might be expected since torchtext does not seem to be developed anymore. From pytorch/text:

WARNING: TorchText development is stopped and the 0.18 release (April 2024) will be the last stable release of the library.

The Installation section mentions PyTorch nightly builds should be compatible with torchtext:main, but I haven’t tested it.