Pytorch: ModuleNotFoundError: No module named 'torch._C'

Created on 24 Jan 2017  ·  40Comments  ·  Source: pytorch/pytorch

Hi there,

I have downloaded the PyTorch pip package CPU version for Python 3.5 from the official webpage. I downloaded it using wget and I renamed the package in order to install the package on ArchLinux with Python 3.6. After that, I used the pip install command to install the package and no error messages are shown. The torchvision package is correctly installed.

If I try to import the torch module using import torch in an ipython session or in a Python session I receive the following error:

Python 3.6.0 (default, Jan 16 2017, 12:12:55) 
[GCC 6.3.1 20170109] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.6/site-packages/torch/__init__.py", line 45, in <module>
    from torch._C import *
ModuleNotFoundError: No module named 'torch._C'

Any ideas?

Best regards,
Alessandro

Most helpful comment

@phenixcx the problem is that you have a folder called torch in the same directory which is being picked up. Do this: cd .. (to change directory), and then start python and import torch, it should work.

All 40 comments

Solution

the problem is that you have a folder called torch in the same directory which is being picked up. Do this: cd .. (to change directory), and then start python and import torch, it should work.


[Edited to add the snippet above, rather than buried down in the thread]

torch._C is the name of our CPython extension (i.e. a C shared library loaded by python). I'm afraid they might not be binary compatible across Python versions. Can you show me the output of ls /usr/lib/python3.6/site-packages/torch/?

you cannot rename pip wheels like that and expect them to work.
This is because the C extensions are named in a way that's specific to the python version.

We shall include pip wheels for python 3.6 from the next release.

@soumith You're definitely right. I have tried it because this procedure works for TensorFlow like a charm :D

@apaszke Here your are:

asuglia@maximillian ~ $ ls -ll /usr/lib/python3.6/site-packages/torch/
totale 43360
drwxr-xr-x 4 root root     4096 24 gen 20.15 autograd
drwxr-xr-x 4 root root     4096 24 gen 20.15 backends
-rwxr-xr-x 1 root root 44131377 24 gen 20.15 _C.cpython-35m-x86_64-linux-gnu.so
drwxr-xr-x 3 root root     4096 24 gen 20.15 cuda
-rwxr-xr-x 1 root root    15612 24 gen 20.15 _dl.cpython-35m-x86_64-linux-gnu.so
-rw-r--r-- 1 root root      942 24 gen 20.15 functional.py
-rw-r--r-- 1 root root     7737 24 gen 20.15 __init__.py
drwxr-xr-x 5 root root     4096 24 gen 20.15 legacy
drwxr-xr-x 3 root root     4096 24 gen 20.15 lib
drwxr-xr-x 3 root root     4096 24 gen 20.15 multiprocessing
drwxr-xr-x 7 root root     4096 24 gen 20.15 nn
drwxr-xr-x 3 root root     4096 24 gen 20.15 optim
drwxr-xr-x 2 root root     4096 24 gen 20.15 __pycache__
-rw-r--r-- 1 root root    12520 24 gen 20.15 serialization.py
drwxr-xr-x 3 root root     4096 24 gen 20.15 sparse
-rw-r--r-- 1 root root     3354 24 gen 20.15 storage.py
-rw-r--r-- 1 root root    31721 24 gen 20.15 _tensor_docs.py
-rw-r--r-- 1 root root    16961 24 gen 20.15 tensor.py
-rw-r--r-- 1 root root    10317 24 gen 20.15 _tensor_str.py
drwxr-xr-x 3 root root     4096 24 gen 20.15 _thnn
-rw-r--r-- 1 root root    98275 24 gen 20.15 _torch_docs.py
drwxr-xr-x 7 root root     4096 24 gen 20.15 utils
-rw-r--r-- 1 root root     2532 24 gen 20.15 _utils.py

If I can give you any other information about the issue feel free to ask, I'm happy to help you!

Yeah, so you can see that there's _C.cpython-35m which means it will be loaded only by Python 3.5. As a quick hack you could try renaming _C, _dl, and _thnn/_THNN to regular .so files (without that cpython-version-artch-platform part, just _C.so). But it's a hack, we don't provide further support if it fails 😄

Thank you for your help! I'll try it :+1:

Hi, I met the same problem when installing PyTorch with Python 2.7.13 和 Anaconda 4.3.0 (64-bit). Would you give me a hint?

The installing way is written as follows:
git clone https://github.com/pytorch/pytorch
pip install -r requirements.txt
python setup.py install

After that, the system told me that PyTorch is installed. Then I wrote down 'python' in the prompt command line. At last, 'import torch' was entered in the python command line. Unfortunately , I got the following information:
Traceback (most recent call last):
File "", line 1, in
File "torch/__init__.py", line 45, in
from torch._C import *
ImportError: No module named _C

The package /usr/lib/python2.7/site-packages has no torch package. The pytorch has a package torch, and has the following files:
[root@AliHPC-M40-434 torch]# ls -ll
total 236
drwxr-xr-x 3 root root 150 Feb 10 11:54 autograd
drwxr-xr-x 3 root root 48 Feb 10 11:54 backends
drwxr-xr-x 9 root root 4096 Feb 10 11:54 csrc
drwxr-xr-x 2 root root 109 Feb 10 11:54 cuda
drwxr-xr-x 2 root root 83 Feb 10 11:54 distributed
-rw-r--r-- 1 root root 2218 Feb 10 11:54 functional.py
-rw-r--r-- 1 root root 7768 Feb 10 11:54 __init__.py
-rw-r--r-- 1 root root 8762 Feb 10 13:49 __init__.pyc
drwxr-xr-x 4 root root 61 Feb 10 11:54 legacy
drwxr-xr-x 15 root root 4096 Feb 10 12:00 lib
drwxr-xr-x 2 root root 93 Feb 10 11:54 multiprocessing
drwxr-xr-x 6 root root 4096 Feb 10 11:54 nn
drwxr-xr-x 2 root root 4096 Feb 10 11:54 optim
-rw-r--r-- 1 root root 12581 Feb 10 11:54 serialization.py
drwxr-xr-x 2 root root 32 Feb 10 11:54 sparse
-rw-r--r-- 1 root root 3354 Feb 10 11:54 storage.py
-rw-r--r-- 1 root root 33716 Feb 10 11:54 _tensor_docs.py
-rw-r--r-- 1 root root 17009 Feb 10 11:54 tensor.py
-rw-r--r-- 1 root root 10346 Feb 10 11:54 _tensor_str.py
drwxr-xr-x 2 root root 71 Feb 10 11:55 _thnn
-rw-r--r-- 1 root root 99370 Feb 10 11:54 _torch_docs.py
drwxr-xr-x 6 root root 145 Feb 10 11:54 utils
-rw-r--r-- 1 root root 2532 Feb 10 11:54 _utils.py
-rw-r--r-- 1 root root 2895 Feb 10 13:49 _utils.pyc

you are not using anaconda python, as it is looking for packages in /usr/lib/python2.7/site-packages. Look at the output of which python and see why it's not the anaconda python

The output of 'which python' is '/root/anaconda2/bin/python'. Just now, I also wrote down the '/root/anaconda2/bin/python' in the command line, and use 'import torch' in the python command line. The result is the same:
Traceback (most recent call last):
File "", line 1, in
File "torch/__init__.py", line 45, in
from torch._C import *
ImportError: No module named _C

In addition, I installed torch 7 in another package before installing pytorch. The OS is centos 7.
Thanks Soumith for help beforehand.

@phenixcx the problem is that you have a folder called torch in the same directory which is being picked up. Do this: cd .. (to change directory), and then start python and import torch, it should work.

Great! It really work. Thank you very much. The package locations make pytorch confused.

@soumith genius observation!

@soumith your suggestion really helps! confused by this problem for a long time. Hope everyone who has the same problem can find this solution successfully.

@soumith Thank you so much I was getting crazy over this error

@soumith Thanks a lot!

Solved my problem too! Thanks @soumith

Gah, I hit this point twice so far :-P . By the way, the issue also arises if you dutifully cd into home directory, but the script itself is in the cloned pytorch folder.

oh god, that was it

worked!

worked!

I got the same error after built from source of branch v0.3.0

My system is OSX10.12, python version is 3.6.1. I don't use anaconda python. I installed all the optional dependency through pip3. I disabled CUDA support by export NO_CUDA=1

Changed the installation command a little bit to fit my system:
Jon (v0.3.0) pytorch $ MACOSX_DEPLOYMENT_TARGET=10.12 CC=clang CXX=clang++ python3 setup.py install

got 7 warnings in the middle of building:
~~/Users/Jon/pytorch/torch/lib/tmp_install/include/THD/base/TensorDescriptor.h:14:29: warning: 'THDTensorDescriptor_newFromTHDoubleTensor' has C-linkage specified, but returns user-defined type
'THDTensorDescriptor' (aka 'at::Tensor') which is incompatible with C [-Wreturn-type-c-linkage]
THD_API THDTensorDescriptor THDTensorDescriptor_newFromTHDoubleTensor(THDoubleTensor *tensor);
^
/Users/Jon/pytorch/torch/lib/tmp_install/include/THD/base/TensorDescriptor.h:15:29: warning: 'THDTensorDescriptor_newFromTHFloatTensor' has C-linkage specified, but returns user-defined type
'THDTensorDescriptor' (aka 'at::Tensor') which is incompatible with C [-Wreturn-type-c-linkage]
THD_API THDTensorDescriptor THDTensorDescriptor_newFromTHFloatTensor(THFloatTensor *tensor);
^
/Users/Jon/pytorch/torch/lib/tmp_install/include/THD/base/TensorDescriptor.h:16:29: warning: 'THDTensorDescriptor_newFromTHLongTensor' has C-linkage specified, but returns user-defined type
'THDTensorDescriptor' (aka 'at::Tensor') which is incompatible with C [-Wreturn-type-c-linkage]
THD_API THDTensorDescriptor THDTensorDescriptor_newFromTHLongTensor(THLongTensor *tensor);
^
/Users/Jon/pytorch/torch/lib/tmp_install/include/THD/base/TensorDescriptor.h:17:29: warning: 'THDTensorDescriptor_newFromTHIntTensor' has C-linkage specified, but returns user-defined type
'THDTensorDescriptor' (aka 'at::Tensor') which is incompatible with C [-Wreturn-type-c-linkage]
THD_API THDTensorDescriptor THDTensorDescriptor_newFromTHIntTensor(THIntTensor *tensor);
^
/Users/Jon/pytorch/torch/lib/tmp_install/include/THD/base/TensorDescriptor.h:18:29: warning: 'THDTensorDescriptor_newFromTHShortTensor' has C-linkage specified, but returns user-defined type
'THDTensorDescriptor' (aka 'at::Tensor') which is incompatible with C [-Wreturn-type-c-linkage]
THD_API THDTensorDescriptor THDTensorDescriptor_newFromTHShortTensor(THShortTensor *tensor);
^
/Users/Jon/pytorch/torch/lib/tmp_install/include/THD/base/TensorDescriptor.h:19:29: warning: 'THDTensorDescriptor_newFromTHCharTensor' has C-linkage specified, but returns user-defined type
'THDTensorDescriptor' (aka 'at::Tensor') which is incompatible with C [-Wreturn-type-c-linkage]
THD_API THDTensorDescriptor THDTensorDescriptor_newFromTHCharTensor(THCharTensor *tensor);
^
/Users/Jon/pytorch/torch/lib/tmp_install/include/THD/base/TensorDescriptor.h:20:29: warning: 'THDTensorDescriptor_newFromTHByteTensor' has C-linkage specified, but returns user-defined type
'THDTensorDescriptor' (aka 'at::Tensor') which is incompatible with C [-Wreturn-type-c-linkage]
THD_API THDTensorDescriptor THDTensorDescriptor_newFromTHByteTensor(THByteTensor *tensor);
^
7 warnings generated.
~
~
Besides these warnings, the installation looks ok to me.
The pytorch source is in ~/pytorch/ directory, I ran python in the ~/Desktop
Any thoughts what may cause this failure of importing?

@Jonbean Hi, I just clone the lateset version of pytorch from master branch. And I have successfully installed it. Why not try master branch?

i`m having the same issue:

ImportErrorTraceback (most recent call last)
in ()
----> 1 import torch

ImportError: No module named 'torch'

i`m using jupyter notebook after install the pytorch, and i dont know how to solve that problem.
note: i use the tensorflow without this problem.

Could you please make this comment more visible?
https://github.com/pytorch/pytorch/issues/574#issuecomment-278879701
e.g. @soumith by editing your 3rd post in this thread?

It's the second time I arrive in this thread and it's the second time that's the solution...

@talesa added the solution to the 2nd post in the thread: https://github.com/pytorch/pytorch/issues/574#issuecomment-274911281

I am having a similar issue. And the solution here doesn't seem to work for me.

I started following the Practical Deep Learning for Coders course at http://course.fast.ai/. I use Windows 7, and Jupyter Notebook started through Anaconda Navigator.

When doing imports at the start of the lesson, I get the following error:

~\Documents\Fast AI\fastai\courses\dl1\fastai\torch_imports.py in <module>()
      1 import os
----> 2 import torch, torchvision, torchtext
      3 from torch import nn, cuda, backends, FloatTensor, LongTensor, optim
      4 import torch.nn.functional as F
      5 from torch.autograd import Variable

C:\ProgramData\Anaconda3\envs\fastai\lib\site-packages\torch\__init__.py in <module>()
     74     pass
     75 
---> 76 from torch._C import *
     77 
     78 __all__ += [name for name in dir(_C)

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

I also tried to do import torch in python outside the Jupyter Notebook:

(fastai) C:\Users\PJvG\Documents\Fast AI\fastai>python
Python 3.6.4 |Anaconda, Inc.| (default, Jan 16 2018, 10:22:32) [MSC v.1900 64 bi
t (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:\ProgramData\Anaconda3\envs\fastai\lib\site-packages\torch\__init__.py
", line 76, in <module>
    from torch._C import *
ImportError: DLL load failed: The specified procedure could not be found.
>>> which python
  File "<stdin>", line 1
    which python
               ^
SyntaxError: invalid syntax

I tried doing cd .., there was no no change.

And I tried it after deactivating my fastai channel:

(base) C:\Users\PJvG\Documents>python
Python 3.6.4 |Anaconda custom (64-bit)| (default, Jan 16 2018, 10:22:32) [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'

Can someone help me with this problem?

@PJvG this seems to be a Windows-related problem. Might be better asking in https://github.com/pytorch/pytorch/issues/494

@PJvG Did you find a solution to your issue?

no, i dont

2018-03-18 0:23 GMT-03:00 karanmurthy7 notifications@github.com:

@PJvG https://github.com/pjvg Did you find a solution to your issue?


You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/pytorch/pytorch/issues/574#issuecomment-373969927,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AWEPqcNsVyhzPm5yfm8enhRUce22vLLtks5tfdMUgaJpZM4Lsrr6
.

I'm trying to get Pytorch to work in Windows 10 with Anaconda. It took a long time to get a successful install. Now when I try to import it I get the same error:

>>> import torch
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\conner\Anaconda3\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.

I've tried cd .. and starting python from that new directory with the same error being raised. I also tried
@apaszke recommendation:

Yeah, so you can see that there's _C.cpython-35m which means it will be loaded only by Python 3.5. As a quick hack you could try renaming _C, _dl, and _thnn/_THNN to regular .so files (without that cpython-version-artch-platform part, just _C.so). But it's a hack, we don't provide further support if it fails 😄

Here's what's in the torch directory:

 Directory of C:\Users\conner\Anaconda3\Lib\site-packages\torch

05/05/2018  07:52 PM    <DIR>          .
05/05/2018  07:52 PM    <DIR>          ..
05/05/2018  07:29 PM    <DIR>          autograd
05/05/2018  07:29 PM    <DIR>          backends
05/05/2018  07:29 PM    <DIR>          contrib
05/05/2018  07:29 PM    <DIR>          cuda
05/05/2018  07:29 PM    <DIR>          distributed
02/24/2018  05:49 AM             6,208 distributions.py
05/05/2018  07:29 PM    <DIR>          for_onnx
02/24/2018  05:49 AM             9,714 functional.py
05/05/2018  07:29 PM    <DIR>          jit
05/05/2018  07:29 PM    <DIR>          legacy
05/05/2018  07:29 PM    <DIR>          lib
05/05/2018  07:29 PM    <DIR>          multiprocessing
05/05/2018  07:29 PM    <DIR>          nn
05/05/2018  07:29 PM    <DIR>          onnx
05/05/2018  07:29 PM    <DIR>          optim
11/09/2017  07:38 PM             4,130 random.py
02/24/2018  05:49 AM            16,759 serialization.py
05/05/2018  07:29 PM    <DIR>          sparse
02/24/2018  05:49 AM             4,016 storage.py
02/24/2018  05:49 AM            14,497 tensor.py
05/05/2018  07:29 PM    <DIR>          utils
02/24/2018  01:46 PM                58 version.py
02/24/2018  01:52 PM        22,208,512 _C.pyd
02/24/2018  01:52 PM            10,240 _nvrtc.cp36-win_amd64.pyd
10/27/2017  09:31 PM             3,044 _six.py
09/17/2017  08:09 AM             1,164 _storage_docs.py
02/24/2018  05:49 AM            39,842 _tensor_docs.py
02/24/2018  05:49 AM            11,024 _tensor_str.py
05/05/2018  07:29 PM    <DIR>          _thnn
02/24/2018  05:49 AM           127,125 _torch_docs.py
02/24/2018  05:49 AM             9,201 _utils.py
02/24/2018  05:49 AM             9,313 __init__.py
05/05/2018  07:29 PM    <DIR>          __pycache__
              16 File(s)     22,474,847 bytes
              19 Dir(s)  318,658,211,840 bytes free

Any ideas on how to get pytorch working?

Hello everyone,
I am totally new to Pytorch, and I have installed it on Anaconda. I have got the same error, and I want to change the directory by cd .... I don't know what this exactly means! Can anyone please tell me how I can do this on Anaconda? and change the directory to what? I am totally confused.

Thanks in advance

My python vision is 3.6.2. I got the same error:

import torch
Traceback (most recent call last):
File "", line 1, in
File "C:UsershpAppDataLocalProgramsPythonPython36libsite-packagestor
ch__init__.py", line 78, in
from torch._C import *
ImportError: DLL load failed: 找不到指定的模块。

Help me please, @connermcb @PJvG @apaszke

hello guys, i am using python 2.7 of my mac system (not third party python)
i faced this problem and got a solution
simply run this command, make sure to adapt it to your environment 👍

sudo install_name_tool -change @rpath/libc++.1.dylib /usr/lib/libc++.1.dylib /Library/Python/2.7/site-packages/torch/_C.so

@phenixcx the problem is that you have a folder called torch in the same directory which is being picked up. Do this: cd .. (to change directory), and then start python and import torch, it should work.

i am on windows and gets the same error any help to solve

problem:
from torch._C import *
ImportError: No module named 'torch._C'

environment:
3.5.6 |Anaconda, Inc.| (default, Aug 26 2018, 16:05:27) [MSC v.1900 64 bit (AMD64)]

solve:
I reinstall torch by pip3 ,then it works.

pip3 install torch==1.2.0+cpu torchvision==0.4.0+cpu -f https://download.pytorch.org/whl/torch_stable.html

@phenixcx the problem is that you have a folder called torch in the same directory which is being picked up. Do this: cd .. (to change directory), and then start python and import torch, it should work.

i am on windows and gets the same error any help to solve

did you find a solution?

Hello everyone!
I have the same problem but solutions do not fit
I user Raspberry pi 3 model B. Exiting the directory does not give a result.
image

Hello everyone!
I have the same problem but solutions do not fit
I user Raspberry pi 3 model B. Exiting the directory does not give a result.
image

Not sure if you managed to tackle this @BulNik , but once you built the wheel, it will be in pytorch/dist folder (whl extension). If you want to link it properly in your path, just need to do this:

pip3 install pytorch/dist/torch-1.6.0a0+68f23d5-cp37-cp37m-linux_armv7l.whl

_replace the wheel file's name with yours_

It works on my Raspberry Pi 4 Model B at least. Let me know if you're still having issues!

After doing the cd... approach, Strangely, restarting and resetting all in my google colabs resolved this issue. Maybe trying this first first could save time. Not sure why though
[No module named 'torch._C' ]

Yeah, so you can see that there's _C.cpython-35m which means it will be loaded only by Python 3.5. As a quick hack you could try renaming _C, _dl, and _thnn/_THNN to regular .so files (without that cpython-version-artch-platform part, just _C.so). But it's a hack, we don't provide further support if it fails

Hi, I made that. And I have a new error :(

import torch
Traceback (most recent call last):
File "", line 1, in
File "/home/pi/.local/lib/python3.7/site-packages/torch/__init__.py", line 79, in
from torch._C import *
ImportError: /usr/lib/arm-linux-gnueabihf/libstdc++.so.6: version `GLIBCXX_3.4.26' not found (required by /home/pi/.local/lib/python3.7/site-packages/torch/lib/libtorch_python.so)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dablyo picture dablyo  ·  3Comments

NgPDat picture NgPDat  ·  3Comments

Coderx7 picture Coderx7  ·  3Comments

bartvm picture bartvm  ·  3Comments

negrinho picture negrinho  ·  3Comments