简体   繁体   中英

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?

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.

重启内核即可解决问题。

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM