“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:28

I had the same problem. In my case I didn't want the GPU version of pytorch. I uninstalled it. The version was pytorch: 0.3.1-py36_cuda80_cudnn6he774522_2 peterjc123.

The problem is that cuda and cudnn . then installed with the following command and now it works!

conda install -c peterjc123 pytorch-cpu
查看更多
手持菜刀,她持情操
3楼-- · 2019-06-20 15:31

I had the same issue with running torch installed with pure pip and solved it by switching to conda. Following steps:

  1. uninstall python 3.6 from python.org (if exists)
  2. install miniconda
  3. install torch in conda ("conda install pytorch -c pytorch")

Issue with pip installation:

import torch
  File "C:\Program Files\Python35\lib\site-packages\torch\__init__.py", line 78, in <module>
    from torch._C import *
ImportError: DLL load failed: The specified module could not be found.

After switching to conda it works fine. I believe the issue was resolved by conda through installing the vs_redist 2017

vs2017_runtime 15.4.27004.2010 peterjc123

But I have tried it w/o conda and it did not help. Could not find how to check (and tweak) Python's vs_redist.

查看更多
冷血范
4楼-- · 2019-06-20 15:36

I also encountered the same problem when I used a conda environment with python 3.6.8 and pytorch installed by conda from channel -c pytorch.

Here is the way the worked for me:

1:) conda create -n envName python=3.6 anaconda

2:) conda update -n envName conda

3:) conda activate envName

4:) conad install pytorch torchvision cudatoolkit=9.0 -c pytorch

and then tested torch with the given code:

5:) python -c "import torch; print(torch.cuda.get_device_name(0))"

Note: 5th step will return your gpu name if you have a cuda compatible gpu

Summery: I just created a conda environment containing whole anaconda and then to tackle the issue of unmatched conda version I updated conda of new environment from the base environment and then installed pytorch in that environment and tested pytorch.

For CPU version here is the link for my another answer: https://gist.github.com/peterjc123/6b804651288e76db7b5fabe5348e1f03#gistcomment-2842825

https://gist.github.com/peterjc123/6b804651288e76db7b5fabe5348e1f03#gistcomment-2842837

查看更多
可以哭但决不认输i
5楼-- · 2019-06-20 15:39

Had the same problem and fixed it by re-installing numpy with mkl (Intel's math kernel library) https://www.lfd.uci.edu/~gohlke/pythonlibs/#numpy

Download the right .whl for your machine. For me it was numpy‑1.14.5+mkl‑cp36‑cp36m‑win_amd64.whl (python 3.6, windows, 64-bit) and then install using pip.

pip install numpy‑1.14.5+mkl‑cp36‑cp36m‑win_amd64.whl
查看更多
相关推荐>>
6楼-- · 2019-06-20 15:41

Make sure you installed the right version of pytorch for your enviroment. I had the same problem I was using pytorch on windows but I had the default package installed which was meant for cuda 8. So I reinstalled the pytorch package for cpu which was what I needed.

查看更多
【Aperson】
7楼-- · 2019-06-20 15:47

I had this similar problem in windows 10...

Solution:

  • Download win-64/intel-openmp-2018.0.0-8.tar.bz2 from https://anaconda.org/anaconda/intel-openmp/files

  • Extract it and put the dll files in Library\bin into
    C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.0\bin

  • Make sure your cuda directory is added to your %PATH% environment variable

查看更多
登录 后发表回答