Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ModuleNotFoundError: No module named 'torch._C' #574

Closed
aleSuglia opened this issue Jan 24, 2017 · 53 comments
Closed

ModuleNotFoundError: No module named 'torch._C' #574

aleSuglia opened this issue Jan 24, 2017 · 53 comments

Comments

@aleSuglia
Copy link

aleSuglia commented Jan 24, 2017

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

@apaszke
Copy link
Contributor

apaszke commented Jan 24, 2017

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/?

@soumith
Copy link
Member

soumith commented Jan 24, 2017

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.

@aleSuglia
Copy link
Author

@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!

@apaszke
Copy link
Contributor

apaszke commented Jan 24, 2017

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 😄

@aleSuglia
Copy link
Author

Thank you for your help! I'll try it 👍

@phenixcx
Copy link

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

@soumith
Copy link
Member

soumith commented Feb 10, 2017

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

@phenixcx
Copy link

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

@phenixcx
Copy link

phenixcx commented Feb 10, 2017

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

@soumith
Copy link
Member

soumith commented Feb 10, 2017

@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.

@phenixcx
Copy link

phenixcx commented Feb 10, 2017

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

@iassael
Copy link

iassael commented Feb 28, 2017

@soumith genius observation!

@Ada-Wu
Copy link

Ada-Wu commented Mar 24, 2017

@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.

@ghost
Copy link

ghost commented Mar 30, 2017

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

@kiminh
Copy link

kiminh commented Apr 27, 2017

@soumith Thanks a lot!

@puzzledqs
Copy link

Solved my problem too! Thanks @soumith

@hughperkins
Copy link
Contributor

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.

@int8
Copy link

int8 commented Aug 23, 2017

oh god, that was it

@DarryNobi
Copy link

worked!

@TomorrowIsAnOtherDay
Copy link

worked!

@Jonbean
Copy link

Jonbean commented Nov 7, 2017

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:

      '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?

@lambdawill
Copy link

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

@EduBrQ
Copy link

EduBrQ commented Nov 22, 2017

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.

@talesa
Copy link

talesa commented Feb 2, 2018

Could you please make this comment more visible?
#574 (comment)
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...

@soumith
Copy link
Member

soumith commented Feb 2, 2018

@talesa added the solution to the 2nd post in the thread: #574 (comment)

@PJvG
Copy link

PJvG commented Feb 14, 2018

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?

@fmassa
Copy link
Member

fmassa commented Feb 14, 2018

@PJvG this seems to be a Windows-related problem. Might be better asking in #494

@karanmurthy7
Copy link

@PJvG Did you find a solution to your issue?

@gauravgoenka
Copy link

@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?

@BulNik
Copy link

BulNik commented Apr 14, 2020

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

@frgfm
Copy link
Contributor

frgfm commented Jun 2, 2020

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!

@Gaussian01
Copy link

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' ]

@jstalahua
Copy link

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)

mcarilli pushed a commit to mcarilli/pytorch that referenced this issue Jan 7, 2021
* refactor tests

* add category based type promotion

* add category test

* re-order test

* clang-tidy; improve integer variance

* flake
KyleCZH pushed a commit to KyleCZH/pytorch that referenced this issue Sep 20, 2021
* CUDA 11.1 Windows builder

* Update driver_update.bat

* update driver

* Update smoke_test.bat
@Makoto1021
Copy link

Hello, I have the same issue with my python script. I'm on a AWS EC2 instance and using Python3.3.9, with torch==1.9.1 and transformers==4.11.3.

Here is my python script:

import torch
from transformers import pipeline, AutoModelForSequenceClassification, BertJapaneseTokenizer, BertTokenizer

And here is the error message. As you can see, it successfully import torch, because I ran this script from a parent folder: python political-tweet-scraper/test.py. But the 2nd line, where I import a bunch of packages from transformers, I get the errorNo module named 'torch._C'.

Traceback (most recent call last):
File "/home/ec2-user/political-tweet-scraper/test.py", line 2, in
from transformers import pipeline, AutoModelForSequenceClassification, BertJapaneseTokenizer, BertTokenizer
File "/home/ec2-user/political-tweet-scraper/env-tweet-scraper/lib/python3.9/site-packages/transformers/file_utils.py", line 2045, in getattr
module = self._get_module(self._class_to_module[name])
File "/home/ec2-user/political-tweet-scraper/env-tweet-scraper/lib/python3.9/site-packages/transformers/file_utils.py", line 2054, in _get_module
return importlib.import_module("." + module_name, self.name)
File "/usr/local/lib/python3.9/importlib/init.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "/home/ec2-user/political-tweet-scraper/env-tweet-scraper/lib/python3.9/site-packages/transformers/pipelines/init.py", line 25, in
from ..models.auto.configuration_auto import AutoConfig
File "/home/ec2-user/political-tweet-scraper/env-tweet-scraper/lib/python3.9/site-packages/transformers/models/init.py", line 19, in
from . import (
File "/home/ec2-user/political-tweet-scraper/env-tweet-scraper/lib/python3.9/site-packages/transformers/models/layoutlm/init.py", line 22, in
from .configuration_layoutlm import LAYOUTLM_PRETRAINED_CONFIG_ARCHIVE_MAP, LayoutLMConfig
File "/home/ec2-user/political-tweet-scraper/env-tweet-scraper/lib/python3.9/site-packages/transformers/models/layoutlm/configuration_layoutlm.py", line 22, in
from ...onnx import OnnxConfig, PatchingSpec
File "/home/ec2-user/political-tweet-scraper/env-tweet-scraper/lib/python3.9/site-packages/transformers/onnx/init.py", line 17, in
from .convert import export, validate_model_outputs
File "/home/ec2-user/political-tweet-scraper/env-tweet-scraper/lib/python3.9/site-packages/transformers/onnx/convert.py", line 23, in
from .. import PreTrainedModel, PreTrainedTokenizer, TensorType, TFPreTrainedModel, is_torch_available
File "/home/ec2-user/political-tweet-scraper/env-tweet-scraper/lib/python3.9/site-packages/transformers/file_utils.py", line 2045, in getattr
module = self._get_module(self._class_to_module[name])
File "/home/ec2-user/political-tweet-scraper/env-tweet-scraper/lib/python3.9/site-packages/transformers/file_utils.py", line 2054, in _get_module
return importlib.import_module("." + module_name, self.name)
File "/usr/local/lib/python3.9/importlib/init.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "/home/ec2-user/political-tweet-scraper/env-tweet-scraper/lib/python3.9/site-packages/transformers/modeling_utils.py", line 27, in
from torch import Tensor, device, nn
File "/home/ec2-user/political-tweet-scraper/env-tweet-scraper/lib/python3.9/site-packages/torch/nn/init.py", line 1, in
from .modules import * # noqa: F403
File "/home/ec2-user/political-tweet-scraper/env-tweet-scraper/lib/python3.9/site-packages/torch/nn/modules/init.py", line 1, in
from .module import Module
File "/home/ec2-user/political-tweet-scraper/env-tweet-scraper/lib/python3.9/site-packages/torch/nn/modules/module.py", line 7, in
from ..parameter import Parameter
File "/home/ec2-user/political-tweet-scraper/env-tweet-scraper/lib/python3.9/site-packages/torch/nn/parameter.py", line 2, in
from torch._C import _disabled_torch_function_impl
ModuleNotFoundError: No module named 'torch._C'

And I cannot locate files named like _C.cpython-. So I don't know how to fix this problem...

(env-tweet-scraper)$ ls env-tweet-scraper/lib/python3.9/site-packages/torch/ -l
total 20
drwxrwxr-x 2 ec2-user ec2-user 246 Dec 28 01:34 lib
drwxrwxr-x 2 ec2-user ec2-user 25 Dec 28 01:34 linalg
drwxrwxr-x 2 ec2-user ec2-user 111 Dec 28 01:34 multiprocessing
drwxrwxr-x 11 ec2-user ec2-user 4096 Dec 28 02:43 nn
drwxrwxr-x 2 ec2-user ec2-user 327 Dec 28 01:34 onnx
drwxrwxr-x 3 ec2-user ec2-user 4096 Dec 28 01:34 optim
drwxrwxr-x 3 ec2-user ec2-user 4096 Dec 28 01:34 package
drwxrwxr-x 2 ec2-user ec2-user 44 Dec 28 01:34 profiler
drwxrwxr-x 4 ec2-user ec2-user 4096 Dec 28 01:34 quantization
drwxrwxr-x 3 ec2-user ec2-user 19 Dec 28 01:34 share
drwxrwxr-x 2 ec2-user ec2-user 25 Dec 28 01:34 sparse
drwxrwxr-x 2 ec2-user ec2-user 25 Dec 28 01:34 special
drwxrwxr-x 3 ec2-user ec2-user 110 Dec 28 01:34 testing
drwxrwxr-x 10 ec2-user ec2-user 4096 Dec 28 01:34 utils

I would appreciate for any help! Thanks

@ndvbd
Copy link

ndvbd commented Jan 25, 2023

Same problem here, python 3.10.6, when doing:
from torch import nn

@frgfm
Copy link
Contributor

frgfm commented Feb 14, 2023

Hi @jstalahua @Makoto1021 @ndvbd 👋

Regarding your respective issues, in all the cases I've seen so far:

  • this specific error means there is a missing folder inside the library folder
  • this folder gets created when the library is built (either if you build from source or if you installed an existing build)
  • another possible thing is that you did install pytorch correctly, but the command is run where there is a folder called "torch" as well (if you cloned the repo, but you do have an installed pytorch version on the side, this error will pop for instance). Because local folders/files have priority in import order before installed libs

So I suggest checking if the problem occurs if you run the command where there is a "torch" folder (if so, run the command somewhere else). And otherwise, install an existing build 😄

@monajalal
Copy link

I have the same exact problem:

(base) mona@ard-gpu-01:~/TensorRT-8.5.3.1/samples/python/network_api_pytorch_mnist$ python
Python 3.9.13 (main, Aug 25 2022, 23:26:10) 
[GCC 11.2.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from torch import nn
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/mona/anaconda3/lib/python3.9/site-packages/torch/nn/__init__.py", line 1, in <module>
    from .modules import *  # noqa: F403
  File "/home/mona/anaconda3/lib/python3.9/site-packages/torch/nn/modules/__init__.py", line 1, in <module>
    from .module import Module
  File "/home/mona/anaconda3/lib/python3.9/site-packages/torch/nn/modules/module.py", line 8, in <module>
    from ..parameter import Parameter
  File "/home/mona/anaconda3/lib/python3.9/site-packages/torch/nn/parameter.py", line 2, in <module>
    from torch._C import _disabled_torch_function_impl
ModuleNotFoundError: No module named 'torch._C'
>>> import torch
>>> 



@frgfm
Copy link
Contributor

frgfm commented Mar 7, 2023

Hi @monajalal 👋

That indeed looks like the same situation. Have you checked my suggestions above?

@generalistbloke
Copy link

Hi!

Can someone help me with the following error? I was trying to install xformers from taking instructions from here and I don't understand where I am going wrong.

:\Stable Diffusion Automatic 1111\stable-diffusion-webui-master\xformers>python setup.py build Traceback (most recent call last): File "E:\Stable Diffusion Automatic 1111\stable-diffusion-webui-master\xformers\setup.py", line 24, in <module> from torch.utils.cpp_extension import ( File "C:\Users\prab-zsbch1060\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\torch\utils\__init__.py", line 4, in <module> from .throughput_benchmark import ThroughputBenchmark File "C:\Users\prab-zsbch1060\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\torch\utils\throughput_benchmark.py", line 2, in <module> import torch._C ModuleNotFoundError: No module named 'torch._C'

Would really appreciate your help!

@frgfm
Copy link
Contributor

frgfm commented May 1, 2023

Hi @generalistbloke 👋

Have you checked my previous answer above?
To check this, open a Python shell somewhere else and try to import pytorch.
If you get the same message, you need to install/reinstall pytorch. If you don't see the message, you most likely have a folder called "torch" in your PATH or at the same location that you got the initial error!

I hope this helps ✌️

@aayushjACN
Copy link

aayushjACN commented Jan 18, 2024

Hi, I have been trying to work with sentence-transformers/all-MiniLM-L12-v2 from hugging face. I pip installed it using the command from hugging face, though I am unable to use it. I am using vscode and am in a virtual environment created using the venv command. Moreover I am using python3.11.7 I tried running the following python code:

from sentence_transformers import SentenceTransformer

and get the following error:

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

Could anyone advise on what I may be doing wrong?

@frgfm
Copy link
Contributor

frgfm commented Jan 21, 2024

Hey @aayushjACN,

Have you tried the solutions in this thread?
Generally speaking, this is only a problem of improper installation of the library (in your venv in this case) or location where the command was run. To help you out:

  1. Try to run import torch; print(torch.__version__) in your venv from your Download folder for instance. If this fails, uninstall and reinstall Pytorch
  2. if it works, share the version you have, how you installed it and the full traceback of your error

That should put us on good tracks to help you out!
Cheers,

@aayushjACN
Copy link

Hey @frgfm,

Thanks for your prompt feedback! I have managed to fix the issue, turns out I just had to restart my VSCode and it worked fine. Must have been some glitch.

Cheers,

@alaaeddinebessghaier
Copy link

Traceback (most recent call last):
File "C:\Users\bessghaier\myenv\yolov5\ByteTrack\setup.py", line 9, in
from torch.utils.cpp_extension import CppExtension
File "C:\Users\bessghaier\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\torch\utils_init_.py", line 4, in
from .throughput_benchmark import ThroughputBenchmark
File "C:\Users\bessghaier\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\torch\utils\throughput_benchmark.py", line 2, in
import torch._C
ModuleNotFoundError: No module named 'torch._C'

@frgfm
Copy link
Contributor

frgfm commented Mar 9, 2024

Hey @alaaeddinebessghaier 👋

We can't do much with the traceback and no context 😅 Mind sharing a bit more? Ideally, we need to be able to reproduce this locally (so minimal reproducible snippet + expected result)

@kavya-seeramsetty
Copy link

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

Yeah, reinstalling works
here are the steps to follow
After you are in your conda environment
pip install ultralytics
conda install pytorch torchvision cpuonly -c pytorch
it will install all the required packages
then run your script -> it will download the weights from yolov8 subsequently captures the frames with bounding boxes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests