Search code examples
pythonjupyter-notebookpytorchjupyter

name '_C' is not defined pytorch+jupyter notebook


I have some code that uses pytorch, that runs fine from my IDE (pycharm).

For research, I tried to run it from a jupyter notebook.

The code in the notebook:

from algorithms import Argparser
from algorithms import Session
def main():
    print("main started")
    args = Argparser.parse()
    session = Session(args)
    session.run()

The package looks like:

|-algorithms
|---__init__.py
|---Argparser.py
|---Session.py
|---<many more files that are being used by Session>.py

some of those files do import torch

When running the code in the notebook, I get

NameError Traceback (most recent call last) in 1 from algorithms import Argparser ----> 2 from algorithms import Session 3 def main(): 4 print("main started") 5 args = Argparser.parse()

D:\git\stav\stav-rl\algorithms\Session.py in 12 13 ---> 14 from algorithms.Episode import Episode 15 from algorithms.Agent import Agent 16 import torch

D:\git\stav\stav-rl\algorithms\Episode.py in 1 author = 'Noam' 2 ----> 3 import torch 4 import numpy as np 5 import cv2

c:\anaconda3\envs\threadartrl\lib\site-packages\torch__init__.py in 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

The error is on from algorithms import Session-->...-->import torch

How can i get the code to run?


Solution

  • You need Cython for pytorch to work:

    pip3 install Cython
    

    See this comment on the issue on github.

    My understanding is that there is a library called _C.cpython-37m-x86_64-linux-gnu.so in site-packages/torch which provides the shared object _C and requires Cython. PyCharm provides Cython support whereas the Jupyter environment doesn't.