9

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?

Gulzar
  • 23,452
  • 27
  • 113
  • 201

4 Answers4

25

Restarting the kernel will solve the problem.

Sankha Jayalath
  • 385
  • 1
  • 6
  • 11
11

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.

Jacques Gaudin
  • 15,779
  • 10
  • 54
  • 75
1

I was not using a notebook, so maybe this is a different issue, but I recently got the same error after upgrading my system libffi. I was able to fix this by installing libffi7.

I went ahead and made a separate question on the assumption this is a different issue, but figured I'd post here in case.

polm23
  • 14,456
  • 7
  • 35
  • 59
0

in my case (google colab), i restarted my runtime and it works.

Runtime > Restart runtime