NameError: name '_C' is not defined while importing torch

I’m getting below error while importing torch version 1.9.0

---> 22 import torch
     23 from tqdm import tqdm
     24 from attrdict import AttrDict

/python/PROJ/torch/1.9.0-py37/exec/lib/torch/__init__.py in <module>()
    227 
    228 
--> 229 __all__ += [name for name in dir(_C)
    230             if name[0] != '_' and
    231             not name.endswith('Base')]

NameError: name '_C' is not defined

Note: imported Cython 0.29.21 version as I see here. But no luck.
I verified that my current working directory is project home, and not torch directory.

Can anyone tell me whats going wrong here?

Are you able to cd into an empty directory and import torch there? If so, I guess some scripts in your current wdir might be using “common” names and could thus interact with Python or PyTorch internals.

@ptrblck

my current working directory is:
/user/rahul/MyApp/examples

I created a notebook in this path and just doing import torch in the notebook. Then, I get the above mentioned error. The directory: /user/rahul/MyApp/examples has no files except the notebook that I created. This error happens only when I change torch version from 1.4.0 to 1.9.0.

I cannot reproduce it with a similar folder structure and the 1.9.0 binaries:

# ~/MyApp/examples$ cat tmp.py 
import torch

print(torch.__version__)

# ~/MyApp/examples$ python tmp.py 
1.9.0

This happened to me a while back. Restarting runtime solved it for me

This error still happening on google colab.

!pip install -q Cython torch

Everyone, Install the Cython first!!!