Cannot import Pytorch [WinError 126] The specified module could not be found

女生的网名这么多〃 提交于 2020-07-18 09:23:31

问题


I'm trying to do a basic install and import of Pytorch/Torchvision on Windows 10. I installed a Anaconda and created a new virtual environment named photo. I opened Anaconda prompt, activated the environment, and I ran:

(photo) C:\Users\<user>\anaconda3\envs>conda install pytorch torchvision cudatoolkit=10.2 -c pytorch**

This installed pytorch successfully. Running conda list I see:

  pytorch            pytorch/win-64::pytorch-1.5.0-py3.7_cuda102_cudnn7_0

  torchvision        pytorch/win-64::torchvision-0.6.0-py37_cu102

Then I open a python command prompt while in the virtual environment, and type:

import torch

The following error is printed:

Traceback (most recent call last): File "", line 1, in File "C:\Users\njord\anaconda3\envs\photo\lib\site-packages\torch__init__.py", line 81, in ctypes.CDLL(dll) File "C:\Users\njord\anaconda3\envs\photo\lib\ctypes__init__.py", line 364, in init self._handle = _dlopen(self._name, mode) OSError: [WinError 126] The specified module could not be found

I have uninstalled/reinstalled python and anaconda but still run into the same issue. Advice appreciated.


回答1:


Refer to below link: https://discuss.pytorch.org/t/cannot-import-torch-on-jupyter-notebook/79334

This is most probably because you are using a CUDA variant of PyTorch on a system that doesn’t have GPU driver installed. That is to say, if you don’t have a Nvidia GPU card, please install the cpu-only package according to the commands on https://pytorch.org.

Conda

conda install pytorch torchvision cpuonly -c pytorch

Pip

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




回答2:


Uninstall your pytorch which you have installed and try this using conda

conda install PyTorch -c PyTorch

If it wasn't work run this code in cmd

pip3 install torchvision



回答3:


Encountered the same issue. I used the fix suggested in by the link below: (Fix 2 worked for me)

https://discuss.pytorch.org/t/winerror-126-when-import-torch/80249/8

Fixes:

1.Update to Python 3.8 from 3.7

2.Apply https://github.com/pytorch/pytorch/pull/37763 manually.

Replace your local copy with https://gist.github.com/peterjc123/bcbf4418ff63d88e11313d308cf1b427 (e.g. C:\ProgramData\Anaconda3\Lib\site-packages\torch)



来源:https://stackoverflow.com/questions/61488902/cannot-import-pytorch-winerror-126-the-specified-module-could-not-be-found

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!