WinError 126 when import torch

Grace_Wu: <p>When I import torch, I receive this error:</p>
<pre><code class="lang-auto">Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  File "C:\Users\Grace Wu\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\torch\__init__.py", line 81, in &lt;module&gt;
    ctypes.CDLL(dll)
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.7_3.7.2032.0_x64__qbz5n2kfra8p0\lib\ctypes\__init__.py", line 364, in __init__
    self._handle = _dlopen(self._name, mode)
OSError: [WinError 126] The specified module could not be found
</code></pre>
<p>My python version is 3.7.7 and I installed pytorch using</p>
<pre><code class="lang-auto">pip install torch==1.5.0+cpu torchvision==0.6.0+cpu -f https://download.pytorch.org/whl/torch_stable.html
</code></pre>
<p>However I did mistakenly install a CUDA version of pytorch before, that I tried to uninstall with</p>
<pre><code class="lang-auto">pip uninstall torch
</code></pre>
<p>Which appeared to have worked, as far as I could tell.<br>
Any assistance would be greatly appreciated.</p>

ptrblck: <p>Based on <a href="https://discuss.pytorch.org/t/cannot-import-torch-on-jupyter-notebook/79334/3">this post</a> from <a class="mention" href="/u/peterjc123">@peterjc123</a> you might still have the CUDA version installed, so I would recommend to run the uninstall command a few times and check the current environment for another PyTorch installation.</p>

Grace_Wu: <p>Thanks for the response!</p>
<p>I made sure to delete all the python files I downloaded on that day, presumed to be part of my initial installation but the error still persists.</p>
<p>Is there another way to ensure a clean uninstallation?<br>
Perhaps there is another source for this error?</p>

ptrblck: <p>Could you try to create a new conda or pip virtual environment, make sure <code>torch</code> cannot be imported there, and reinstall the CPU packages?</p>

Grace_Wu: <p>Hi Peter, I repeated the steps in a virtual environment and am getting the same error.</p>
<pre><code class="lang-auto">C:\Users\Grace Wu&gt;torchenv\Scripts\activate

(torchenv) C:\Users\Grace Wu&gt;python
Python 3.7.7 (tags/v3.7.7:d7c567b08f, Mar 10 2020, 11:52:54) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
&gt;&gt;&gt; import torch
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
ModuleNotFoundError: No module named 'torch'
&gt;&gt;&gt; quit()

(torchenv) C:\Users\Grace Wu&gt;pip install torch==1.5.0+cpu torchvision==0.6.0+cpu -f https://download.pytorch.org/whl/torch_stable.html
Looking in links: https://download.pytorch.org/whl/torch_stable.html
Collecting torch==1.5.0+cpu
  Using cached https://download.pytorch.org/whl/cpu/torch-1.5.0%2Bcpu-cp37-cp37m-win_amd64.whl (109.2 MB)
Collecting torchvision==0.6.0+cpu
  Using cached https://download.pytorch.org/whl/cpu/torchvision-0.6.0%2Bcpu-cp37-cp37m-win_amd64.whl (456 kB)
Processing c:\users\grace wu\appdata\local\pip\cache\wheels\56\b0\fe\4410d17b32f1f0c3cf54cdfb2bc04d7b4b8f4ae377e2229ba0\future-0.18.2-py3-none-any.whl
Collecting numpy
  Downloading numpy-1.18.4-cp37-cp37m-win_amd64.whl (12.8 MB)
     |████████████████████████████████| 12.8 MB 2.2 MB/s
Collecting pillow&gt;=4.1.1
  Using cached Pillow-7.1.2-cp37-cp37m-win_amd64.whl (2.0 MB)
Installing collected packages: future, numpy, torch, pillow, torchvision
Successfully installed future-0.18.2 numpy-1.18.4 pillow-7.1.2 torch-1.5.0+cpu torchvision-0.6.0+cpu

(torchenv) C:\Users\Grace Wu&gt;python
Python 3.7.7 (tags/v3.7.7:d7c567b08f, Mar 10 2020, 11:52:54) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
&gt;&gt;&gt; import torch
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  File "C:\Users\Grace Wu\torchenv\lib\site-packages\torch\__init__.py", line 81, in &lt;module&gt;
    ctypes.CDLL(dll)
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.7_3.7.2032.0_x64__qbz5n2kfra8p0\lib\ctypes\__init__.py", line 364, in __init__
    self._handle = _dlopen(self._name, mode)
OSError: [WinError 126] The specified module could not be found
&gt;&gt;&gt;
</code></pre>
<p>Using print statements, I find that the DLL file that is causing problems is caffe2_detectron_ops_gpu.dll<br>
What might this mean?<br>
pip uninstall caffe2 tells me that caffe2 is not installed</p>

peterjc123: <p>Please use the commands below:</p>
<pre><code class="lang-auto">"C:\Users\Grace Wu\torchenv\python.exe" -m pip uninstall torch torchvision
"C:\Users\Grace Wu\torchenv\python.exe" -m pip install torch==1.5.0+cpu torchvision==0.6.0+cpu -f https://download.pytorch.org/whl/torch_stable.html
</code></pre>
<p>If it doesn’t work, then please manually remove the DLLs in <code>C:\Users\Grace Wu\torchenv\lib\site-packages\torch\lib</code> after you uninstall PyTorch.</p>

Grace_Wu: <p>Hi,</p>
<p>The same error persists after I execute both commands.<br>
I cannot manually remove the DLLs after I uninstall PyTorch because the whole site-packages\torch folder is gone so I guess that meant my uninstallation seemed to work.<br>
Any other possible reasons for this other than problems with my initial installation?</p>
<p>I noticed that this is an issue that several users have been experiencing recently, any reason for that?</p>
<p>Thanks,<br>
Grace</p>

peterjc123: <p>Normally, this error means that one or more dependent DLL is missing during the initialization, which mostly caused by users that tend to load the CUDA variant of PyTorch on a CPU-only agent. But apparently, your problem is a bit different.</p>
<p>Update:<br>
From the location of your python executable, it seems that you are using the Python distribution from the Microsoft Store. We didn’t test our binaries under this case so there may be some problem. Would you please provide a detailed version of the log using the steps below?</p>
<ol>
<li>Install Debugging Tools on Windows <a href="https://docs.microsoft.com/en-us/windows-hardware/drivers/debugger/#install-debugging-tools-for-windows" rel="nofollow noopener">https://docs.microsoft.com/en-us/windows-hardware/drivers/debugger/#install-debugging-tools-for-windows</a>
</li>
<li>Run the following code in CMD<pre><code class="lang-auto"># Turn on loader snaps
gflags /i python.exe +sls
# Debug loading torch
cdb -o -c "~*g; q" python -c "import torch"
# Turn off loader snaps
gflags /i python.exe +sls
</code></pre>
</li>
</ol>

peterjc123: <p>Well, I reproduced it locally. Looks like it could not find our internal DLLs because it relies on <code>AddDllDirectory</code> which is not built-in in Python 3.7. There are two workable solutions:</p>
<ol>
<li>Update to Python 3.8.</li>
<li>Apply <a href="https://github.com/pytorch/pytorch/pull/37763" rel="nofollow noopener">https://github.com/pytorch/pytorch/pull/37763</a> manually. Replace your local copy with <a href="https://gist.github.com/peterjc123/bcbf4418ff63d88e11313d308cf1b427" rel="nofollow noopener">https://gist.github.com/peterjc123/bcbf4418ff63d88e11313d308cf1b427</a> (e.g. <code>C:\Users\Grace Wu\torchenv\lib\site-packages\torch\__init__.py</code>)</li>
</ol>

Grace_Wu: <p>This fixed my problem! Thank you for your help.</p>

Musiclusive: <p>Can you please tell me how you solved it. I’m getting the same error.</p>

nsovilj: <p>I have installed Microsoft Build Tools from <a href="https://visualstudio.microsoft.com/visual-cpp-build-tools/" rel="nofollow noopener">https://visualstudio.microsoft.com/visual-cpp-build-tools/</a><br>
I have checked all boxes regarding C++ section.</p>
<p>Before this I could only import torch from jupyter notebook but not from ipython or python shell. I don’t why that’s so but it works now.</p>