Can't import torch even with pytorch installed

I’m trying to install Wan2.1 and part of the requirements are to make sure PyTorch is installed. When installing the requirements, I see this:

Collecting flash_attn (from -r requirements.txt (line 14))
  Using cached flash_attn-2.8.0.post2.tar.gz (7.9 MB)
  Preparing metadata (setup.py) ... error
  error: subprocess-exited-with-error
  
  × python setup.py egg_info did not run successfully.
  │ exit code: 1
  ╰─> [14 lines of output]
      Traceback (most recent call last):
        File "<string>", line 2, in <module>
        File "<pip-setuptools-caller>", line 35, in <module>
        File "/tmp/pip-install-iry0zzsu/flash-attn_822fcc6b33334bee9b9beadb1b2f0ea3/setup.py", line 22, in <module>
          import torch
        File "/home/jagar005/.local/lib/python3.9/site-packages/torch/__init__.py", line 404, in <module>
          _load_global_deps()
        File "/home/jagar005/.local/lib/python3.9/site-packages/torch/__init__.py", line 360, in _load_global_deps
          raise err
        File "/home/jagar005/.local/lib/python3.9/site-packages/torch/__init__.py", line 318, in _load_global_deps
          ctypes.CDLL(global_deps_lib_path, mode=ctypes.RTLD_GLOBAL)
        File "/usr/lib64/python3.9/ctypes/__init__.py", line 374, in __init__
          self._handle = _dlopen(self._name, mode)
      OSError: /home/jagar005/.local/lib/python3.9/site-packages/torch/lib/libtorch_global_deps.so: failed to map segment from shared object
      [end of output]
  
  note: This error originates from a subprocess, and is likely not a problem with pip.
error: metadata-generation-failed

× Encountered error while generating package metadata.
╰─> See above for output.

I initially thought it might be a permissions error (from similar threads), but when I launch the Python console, and try an import torch, I get a ModuleNotFoundError.

cc @ProGamerGov because I saw them solve another similar case, I hope that’s okay.

I guess you did not install any PyTorch binary or are using the wrong environment. Check where PyTorch was installed via pip show torch which will print the install location and make sure it’s the right virtual environment.

Hi @ptrblck, thank you for your response. You might be right, it’s installed here:

Location: /home/jagar005/.local/lib/python3.9/site-packages

Which isn’t the virtual environment I’d set up (that’s called wan21). How can I address this?

Activate the desired environment. E.g. if you are using conda use conda activate wan21. Then install PyTorch inside this env.

EDIT: if you have installed PyTorch also in the base env, I would recommend removing it and only using virtual envs.

So I deactivated the environment and wiped the entire directory, including the virtual environment. I then uninstalled torch, and created everything anew. When in the environment, I ran this command:

pip install torch==2.6.0 torchvision==0.21.0  --index-url https://download.pytorch.org/whl/cu126

and when I run pip show torch, it shows this output:

Name: torch
Version: 2.6.0+cu126
Summary: Tensors and Dynamic neural networks in Python with strong GPU acceleration
Home-page: https://pytorch.org/
Author: PyTorch Team
Author-email: packages@pytorch.org
License: BSD-3-Clause
Location: /home/jagar005/.local/lib/python3.9/site-packages
Requires: filelock, fsspec, jinja2, networkx, nvidia-cublas-cu12, nvidia-cuda-cupti-cu12, nvidia-cuda-nvrtc-cu12, nvidia-cuda-runtime-cu12, nvidia-cudnn-cu12, nvidia-cufft-cu12, nvidia-curand-cu12, nvidia-cusolver-cu12, nvidia-cusparse-cu12, nvidia-cusparselt-cu12, nvidia-nccl-cu12, nvidia-nvjitlink-cu12, nvidia-nvtx-cu12, sympy, triton, typing-extensions
Required-by: accelerate, torchvision

So it just goes to the wrong place.