Search code examples
pythonpytorch

name '_C' is not defined


I tried to import pytorch in Jupyter notebook: import torch

When I run it I get this error message:

NameError                                 Traceback (most recent call last)
<ipython-input-7-db4b44599dae> in <module>
----> 1 import torch
      2 import numpy as np
      3 from torch import nn
      4 from tqdm.auto import tqdm
      5 from torchvision import transforms

/opt/anaconda3/lib/python3.8/site-packages/torch/__init__.py in <module>
    463     raise  # If __file__ is not None the cause is unknown, so just re-raise.
    464 
--> 465 for name in dir(_C):
    466     if name[0] != '_' and not name.endswith('Base'):
    467         __all__.append(name)

NameError: name '_C' is not defined

I have tried potential solutions but nothing worked. I tried deleting and reinstalling Pytorch and Cython, but I kept getting the same error.


Solution

  • I had this same error, I found the issue was an outdated package. I had to manually specify the latest typing_extensions:

    pip3 install typing_extensions==4.9.0