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

username: Atta
cooked: <p>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.</p>
<pre><code class="lang-auto">segmenation fault (core dumped)
</code></pre>
<p>I also created a virtual environment and installed torchtext with pip. But was not able to make it work.</p>
<pre><code class="lang-auto">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
----&gt; 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
---&gt; 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
---&gt; 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.")
---&gt; 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
---&gt; 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.
-&gt; 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:
--&gt; 379     self._handle = _dlopen(self._name, mode)
    380 else:
    381     self._handle = handle


</code></pre>
<p>Any help will be appreciated.</p>

username: Bikash_Kharel
cooked: <p>Version mismatch problem; in the conda environment, do the following:<br>
pip uninstall torch<br>
pip install torch=2.3.0<br>
pip install torchtext=0.18</p>

username: Atta
cooked: <p>Thank you for pointing the version mismatch out. Just a small fix in terms syntax (at least in my case):</p>
<pre><code class="lang-auto">pip install torch==2.3.0
pip install torchtext==0.18
</code></pre>
<p>with two equal signs <code>==</code> instead of single <code>=</code>.</p>

username: Bikash_Kharel
cooked: <p>Thanks for correcting the typo I made.</p>

username: MuhammadShahmirZaman
cooked: <p>Thank you for resolving the problem.</p>