3

I'm getting the following error when trying to import torch in Jupyter:

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-10-39178> in <module>
      1 
----> 2 import torch

~/jupyter_py3/lib/python3.6/site-packages/torch/__init__.py in <module>
     84 from torch._C import *
     85 
---> 86 __all__ += [name for name in dir(_C)
     87             if name[0] != '_' and
     88             not name.endswith('Base')]

NameError: name '_C' is not defined


     88             not name.endswith('Base')]

NameError: name '_C' is not defined

Cython is already installed, in case you may be wondering.

Any idea how to fix this?


UPDATE:

Three years later, I'm getting the same error when attempting to import torch in Python 3.8.0!

running it from an empty dir makes it go passed this issue. i.e. try the following

mkdir empty_dir && cd empty_dir && python -c 'import torch'

source: answer

Also, I just wan to put it out there that the root cause of this issue for me when importing torch was a broken numpy installation. The thing that ended up solving the entirety of the issue was a simple pip install numpy==1.23.2

Zahra
  • 6,798
  • 9
  • 51
  • 76
  • Does this answer your question? [name '\_C' is not defined pytorch+jupyter notebook](https://stackoverflow.com/questions/54408973/name-c-is-not-defined-pytorchjupyter-notebook) – polm23 Nov 25 '21 at 07:11
  • @polm23 Nope! :-/ – Zahra Aug 16 '22 at 23:51
  • useful source: https://discuss.pytorch.org/t/nameerror-name-c-is-not-defined-while-importing-torch/124721 – Zahra Aug 16 '22 at 23:51

0 Answers0