Can't import torch even with pytorch installed

easythrees: <p>I’m trying to install <code>Wan2.1</code> and part of the requirements are to make sure PyTorch is installed. When installing the requirements, I see this:</p>
<pre><code class="lang-auto">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
  ╰─&gt; [14 lines of output]
      Traceback (most recent call last):
        File "&lt;string&gt;", line 2, in &lt;module&gt;
        File "&lt;pip-setuptools-caller&gt;", line 35, in &lt;module&gt;
        File "/tmp/pip-install-iry0zzsu/flash-attn_822fcc6b33334bee9b9beadb1b2f0ea3/setup.py", line 22, in &lt;module&gt;
          import torch
        File "/home/jagar005/.local/lib/python3.9/site-packages/torch/__init__.py", line 404, in &lt;module&gt;
          _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.
╰─&gt; See above for output.
</code></pre>
<p>I initially thought it might be a permissions error (from similar threads), but when I launch the Python console, and try an <code>import torch</code>, I get a <code>ModuleNotFoundError</code>.</p>
<p>cc <a class="mention" href="/u/progamergov">@ProGamerGov</a> because I saw them solve another similar case, I hope that’s okay.</p>

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

easythrees: <p>Hi <a class="mention" href="/u/ptrblck">@ptrblck</a>, thank you for your response. You might be right, it’s installed here:</p>
<pre><code class="lang-auto">Location: /home/jagar005/.local/lib/python3.9/site-packages
</code></pre>
<p>Which isn’t the virtual environment I’d set up (that’s called <code>wan21</code>). How can I address this?</p>

ptrblck: <p>Activate the desired environment. E.g. if you are using <code>conda</code> use <code>conda activate wan21</code>. Then install PyTorch inside this env.</p>
<p>EDIT: if you have installed PyTorch also in the base env, I would recommend removing it and only using virtual envs.</p>

easythrees: <p>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:</p>
<pre><code class="lang-auto">pip install torch==2.6.0 torchvision==0.21.0  --index-url https://download.pytorch.org/whl/cu126
</code></pre>
<p>and when I run <code>pip show torch</code>, it shows this output:</p>
<pre><code class="lang-auto">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
</code></pre>
<p>So it just goes to the wrong place.</p>