WinError 126 when import torch

When I import torch, I receive this error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  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 <module>
    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

My python version is 3.7.7 and I installed pytorch using

pip install torch==1.5.0+cpu torchvision==0.6.0+cpu -f https://download.pytorch.org/whl/torch_stable.html

However I did mistakenly install a CUDA version of pytorch before, that I tried to uninstall with

pip uninstall torch

Which appeared to have worked, as far as I could tell.
Any assistance would be greatly appreciated.

2 Likes

Based on this post from @peterjc123 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.

Thanks for the response!

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.

Is there another way to ensure a clean uninstallation?
Perhaps there is another source for this error?

Could you try to create a new conda or pip virtual environment, make sure torch cannot be imported there, and reinstall the CPU packages?

Hi Peter, I repeated the steps in a virtual environment and am getting the same error.

C:\Users\Grace Wu>torchenv\Scripts\activate

(torchenv) C:\Users\Grace Wu>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.
>>> import torch
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'torch'
>>> quit()

(torchenv) C:\Users\Grace Wu>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>=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>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.
>>> import torch
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\Grace Wu\torchenv\lib\site-packages\torch\__init__.py", line 81, in <module>
    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
>>>

Using print statements, I find that the DLL file that is causing problems is caffe2_detectron_ops_gpu.dll
What might this mean?
pip uninstall caffe2 tells me that caffe2 is not installed

Please use the commands below:

"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

If it doesn’t work, then please manually remove the DLLs in C:\Users\Grace Wu\torchenv\lib\site-packages\torch\lib after you uninstall PyTorch.

Hi,

The same error persists after I execute both commands.
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.
Any other possible reasons for this other than problems with my initial installation?

I noticed that this is an issue that several users have been experiencing recently, any reason for that?

Thanks,
Grace

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.

Update:
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?

  1. Install Debugging Tools on Windows https://docs.microsoft.com/en-us/windows-hardware/drivers/debugger/#install-debugging-tools-for-windows
  2. Run the following code in CMD
    # 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
    

Well, I reproduced it locally. Looks like it could not find our internal DLLs because it relies on AddDllDirectory which is not built-in in Python 3.7. There are two workable solutions:

  1. Update to Python 3.8.
  2. Apply https://github.com/pytorch/pytorch/pull/37763 manually. Replace your local copy with https://gist.github.com/peterjc123/bcbf4418ff63d88e11313d308cf1b427 (e.g. C:\Users\Grace Wu\torchenv\lib\site-packages\torch\__init__.py)

This fixed my problem! Thank you for your help.

Can you please tell me how you solved it. I’m getting the same error.

I have installed Microsoft Build Tools from https://visualstudio.microsoft.com/visual-cpp-build-tools/
I have checked all boxes regarding C++ section.

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.