“import torch” giving error “from torch._C import

2019-06-20 15:02发布

I am currently using Python 3.5.5 on Anaconda and I am unable to import torch. It is giving me the following error in Spyder:

Python 3.5.5 |Anaconda, Inc.| (default, Mar 12 2018, 17:44:09) [MSC v.1900 
64 bit (AMD64)]
Type "copyright", "credits" or "license" for more information.

IPython 6.2.1 -- An enhanced Interactive Python.

import torch
Traceback (most recent call last):

  File "<ipython-input-1-eb42ca6e4af3>", line 1, in <module>
    import torch

  File "C:\Users\trish\Anaconda3\envs\virtual_platform\lib\site-
  packages\torch\__init__.py", line 76, in <module>
    from torch._C import *

ImportError: DLL load failed: The specified module could not be found.

Many suggestions on the internet say that the working directory should not be the same directory that the torch package is in, however I've manually set my working directory to C:/Users/trish/Downloads, and I am getting the same error.

Also I've already tried the following: reinstalling Anaconda and all packages from scratch, and I've ensured there is no duplicate "torch" folder in my directory.

Pls help! Thank you!

标签: python torch
7条回答
放荡不羁爱自由
2楼-- · 2019-06-20 15:47

Windows10 Solution(This worked for my system):

I was having the same issue in my system. Previously I was using Python 3.5 and I created a virtual environment named pytorch_test using the virtualenv module because I didn't want to mess up my tensorflow installation(which took me a lot of time). I followed every instruction but it didn't seem to work. I installed python 3.6.7 added it to the path. Then I created the virtual environment using:

virtualenv --python=3.6 pytorch_test

Then go to the destination folder

cd D:\pytorch_test

and activate the virtual environment entering the command in cmd:

.\Scripts\activate

After you do this the command prompt will show:

(pytorch_test) D:\pytorch_test>

Update pip if you have not done it before using:

(pytorch_test) D:\pytorch_test>python -m pip install --upgrade pip

Then go for installing numpy+mkl from the site:

https://www.lfd.uci.edu/~gohlke/pythonlibs/#numpy

Choose the correct version from the list if you have python 3.6.7 go with the wheel file: numpy‑1.15.4+mkl‑cp36‑cp36m‑win_amd64.whl (For 64 bit)

(Note if the whole thing doesnot work just go with simple numpy installation and mkl installation separately) Then go for installing openmp using:

(pytorch_test) D:\pytorch_test>pip install intel-openmp

Now you are done with the prerequisites. To install pytorch go to the previous versions site: https://pytorch.org/get-started/previous-versions/

Here select the suitable version from the list of Windows Binaries. For example I am having CUDA 9.0 installed in my system with python 3.6.7 so I went with the gpu version:

cu90/torch-1.0.0-cp36-cp36m-win_amd64.whl

(There are two available versions 0.4.0 and 1.0.0 for pytorch, I went with 1.0.0)

After downloading the file install it using pip(assuming the whl file is in D:).You have to do this from the virtual environment pytorch_test itself:

(pytorch_test) D:\pytorch_test>pip install D:\torch-1.0.0-cp36-cp36m-win_amd64.whl

Prerequisites like six, pillow will be installed automatically. Then once everything is done, install the models using torchvision. Simply type :

(pytorch_test) D:\pytorch_test>pip install torchvision

To check everything is working fine try the following script:

import torch
test = torch.rand(4, 7)
print(test)

If everything was good then it wont be an issue. Whenever there is an issue like this it is related to version mismatch of one or more dependencies. This also occurred during tensorflow installation.

Deactivate the following virtual environment using the command deactivate in the cmd:

(pytorch_test) D:\pytorch_test>deactivate

This is the output of pip list in my system:


Package      Version
------------ -----------
intel-openmp 2019.0
mkl          2019.0
numpy        1.16.2
Pillow       6.0.0
pip          19.0.3
setuptools   41.0.0
six          1.12.0
torch        1.0.0
torchvision  0.2.2.post3
wheel        0.33.1

Hope this helps. This is my first answer in this community, hope you all find it helpful. I setup pytorch today in the afternoon after trying all sorts of combinations. The same import problem occurred to me while installing CNTK and tensorflow. Anyway I kept them separate in different virtual environments so that I can use them anytime.

查看更多
登录 后发表回答